0

Below is a copy of my data. I've been trying to find the last column + 1 (so in this example, it should be 4, since last column is C and plus 1). The code I'm using is to determine last column is:

lastColumn = Cells(lastRow, Columns.Count).End(xlToLeft).Column

But I keep getting 2?

enter image description here

akh275
  • 15
  • 5
  • 2
    I assume `lastRow` is 10 (your last row of data), and in that row, only 2 columns are filled. Use the row where you have your header (5). Or dig into https://stackoverflow.com/a/11169920/7599798 – FunThomas Sep 17 '21 at 12:27
  • @FunThomas my god, i can't believe i didn't see this. Yep it worked, thank you!! – akh275 Sep 17 '21 at 12:31

2 Answers2

0

lastColumn = Cells(5, Columns.Count).End(xlToLeft).Column

result will be 3 (last column on row 5)

lastColumn = Cells(lastRow, Columns.Count).End(xlToLeft).Column

result will be 2 (last column on row 10)

Alexey C
  • 172
  • 6
0
lastColumn = Cells(**5**, Columns.Count).End(xlToLeft).Column 

Was using lastrow and in that row, only two columns were filled. Used the header row instead.

ouflak
  • 2,458
  • 10
  • 44
  • 49
akh275
  • 15
  • 5