using Name Column in VBA to paste

dlz217

New Member
Joined
Aug 23, 2013
Messages
49
I'm doing a large copy/paste macro from an input form into a data table. I'm still designing both, so I'd like to use a named column in my data table rather than a set column letter.

how can I update the "F" in this so it uses a named column like lastCol instead?

VBA Code:
With dataWks
          nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
      End With

dataWks.Cells(nextRow, "F").Value = inputWks.Range("lastIn").Value
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try using this, which will find the last column of used data
VBA Code:
Dim lc As Integer
lc = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
With dataWks
          nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
      End With

dataWks.Cells(nextRow, lc).Value = inputWks.Range("lastIn").Value
 
Upvote 0
No, I have about 50 fields that are named like this. I want to name the columns too where the value will be pasted so that I can manipulate the data table later on.
 
Upvote 0
Ok, I'm a little confused...do you want the user to specify which column they want to use ??
 
Upvote 0
No. What Id like to do is update the code so instead of using "F" in the code for the destination, it uses a named range for the column such as "StreetCol". That way I can rearrange the columns afterwards and not have to change the VBA.
 
Upvote 0
Do you mean like this??
VBA Code:
Dim lastcol As Integer
lastcol = [StreetCol].Column
With dataWks
          nextrow = .Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Row
      End With
dataWks.Cells(nextrow, lastcol).Value = inputWks.Range("lastIn").Value
End Sub
 
Upvote 0
Yes. Is there a way to make it more simple, in line, without having to Dim name each Integer variable like that? I have 50+ data points. Could you do something like

VBA Code:
dataWks.Cells(nextrow, [StreetCol].Column).Value = inputWks.Range("lastIn").Value

where it's just right there? Cause I have a ton of these:

Code:
.Cells(nextRow, "C").Value = inputWks.Range("prefixIn").Value
          .Cells(nextRow, "D").Value = inputWks.Range("firstIn").Value
          .Cells(nextRow, "E").Value = inputWks.Range("middleIn").Value
          .Cells(nextRow, "F").Value = inputWks.Range("lastIn").Value
          .Cells(nextRow, "G").Value = inputWks.Range("suffixIn").Value
          .Cells(nextRow, "I").Value = inputWks.Range("liscIn").Value
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top