How to get last column used in excel using vba [duplicate] - vba

This question already has answers here:
Find last used cell in Excel VBA
(14 answers)
Closed 5 years ago.
Hi I am having issues getting to the last column used in my sheet.
I do know that as it stands my last column is 43 and i want it to enter a value at column 44, so then when i search again it will go to column 44 as last column used and then enter a value at 45 and so on and so on. I am using a ref num to find the correct row.
The code I am using is as follows to try get the last column
'find last empty Col in database
LastCol = sou.Cells(2, Columns.count).End(xlToLeft).Column + 1
MsgBox LastCol
'For Loop to cycle through the columns
For x = 2 To LastCol
'If the row value at column 1 = the ref number then continue
If sou.Cells(x, 1).Value = refNum Then
sou.Cells(x, LastCol).Value = Me.commentBox.Text
Exit For
End If
Next
The issue is that for some reason it only goes to column 22. I have tried to use the other ways to get the last column but they have just given me the last column in the whole entire sheet which again is not what I want.
If someone can lend me a hand as I'm a newbie to this it would be greatly appreciated!

To find the last row used, have you tried
lastRow = ActiveSheet.UsedRange.Rows.Count
If that didn't work, please elaborate what went wrong and I can submit more code.

Related

Efective method to find last row vba excel [duplicate]

This question already has answers here:
Find last used cell in Excel VBA
(14 answers)
Closed 4 years ago.
i have the following method to set to define a ranga:
Set RegJan = Sheets("BD").Range("T3:T50000")
the range is smaller than this, but in order to make it work for some months, i set it to 50000 rows.
i then use this code in the following line of vba:
ws.Cells(8, 2).Value = Application.WorksheetFunction.SumIfs(RegJan,
EquipaBd, EquipaForm, AgenteBd, AgenteForm) 'Soma dos Registos
This last line i already transformed in a loop so that it calculates in every line i need it,the problem is that i have to do the first line of code for every month of the year an for 6 more variables
is there a better way of doing this? possibly a dynamic way?
This might give you some ideas:
With Sheets("BD")
' Since T is column #20 and goes to 6 more
For col = 20 To 26
' get last row
lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
' set range
Set yourRng = .Range(.Cells(3, col), .Cells(lastRow, col))
' you might want to change 8,2 to make dynamic
ws.Cells(8, 2).Value = Application.WorksheetFunction.SumIfs(yourRng , EquipaBd, EquipaForm, AgenteBd, AgenteForm)
Next
End With

Delete Blank Cells And Shift Up Fail [duplicate]

This question already has answers here:
Delete blank cells in range
(6 answers)
Closed 4 years ago.
I've searched and tried alooot of solutions regarding this issue. But non of them worked. So im trying to attach the excel file here.
My issue is:
Column A
1324
12312
14
4323
12
11234
I want B to look like:
Column B
1324
12312
14
4323
12
11234
Looks simple. But it doesn't work since the Blank cells doesn't actually appear to blank. And I cant find a way to get rid of them. I'm attaching the excel file for your reference.
Excel File:
https://drive.google.com/open?id=1PDskY1GJKYhzzj905KrX988F8tTaNQSs
I believe the following will achieve your desired result, simply copy the code under your command button.
This will loop through row 1 to Last on Column A and if the cell is not blank it will pass the value to the next free row on Column B:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set the worksheet you are working with, amend as required
LastRowA = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
For i = 1 To LastRowA
'loop from row 1 to Last
LastRowB = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row + 1
'get the last row with data on Column B and offset by one (next empty row)
If ws.Cells(i, "A").Value <> "" Then ' if Column A's value is not empty
ws.Cells(LastRowB, "B").Value = ws.Cells(i, "A").Value 'pass that value to the next available row on Column B
End If
Next i
End Sub

VBA - Copy and Paste with increment

Hi i need help on creating a VBA to copy range of cells repeatedly with one column having increments.
Current data
Expected Output
I found a vba but will only copy the rows based on column C without increments on the date
Excel VBA automation - copy row "x" number of times based on cell value
I'm not sure why you would need to do this as excel has built-pattern pattern recognition for these scenarios, after entering two succesive dates if you hover over and click the border of the cell then drag down, the desired date range will appear in the column automatically.
If you still insist on doing this programatically for whatever reason then your question already has multiply feasible solutions here: Add one day to date in cells using VBA
Specify each cell in turn then increment the value by the corresponding row number to return desired date range in column A:
Range("A2").Value = Range("A2").Value + 2 ' add 2 days
Range("A3").Value = Range("A3").Value + 3 ' add 3 days
Range("A4").Value = Range("A4").Value + 4 ' add 4 days
'-------- So on and so forth until desired range is acheived --------'
or alternatively:
Range("A2").value = DateAdd("d", 2, CDate(Range("A2")))
Range("A3").value = DateAdd("d", 3, CDate(Range("A3")))
Speaking as someone who had to learn the hard way, please take my advice and ensure you research your problem thouroughly to find a solution before posting. Refer to the guidelines here if needed.

Finding last used row in a table then adding data to the remaining rows below

I've been googling this one for a while now with no success - I have a table with around 850 rows and in columns 8 to 13 I need to find the last used row, and then add data to the remaining empty rows below (if any). I've found plenty of examples working with ranges but they all just find the last row in the table and as such don't work for me. I'm a beginner with VBA and I've tried working with the ListColumns object but to no avail, and as such have no current code to post. Can anyone provide some insight into this?
This will do it:
Sub Macro1()
Dim i, iColumn, Ending_Column As Long ' Declare you long data types
Ending_Column = 13 ' Assing the right-most column
i = Range("H" & Range("H" & Rows.Count).End(xlUp).Row) + 1 ' Find the next row column H (this lets you enter the data on the same line)
For iColumn = 8 To Ending_Column ' Asign left-most column and loop to the right-most one
Cells(i, iColumn).Value = "Your input" ' The "Your input" needs to be modified
Next ' next column
End Sub

Adjust criteria to depend on multiple values

I have been working on a code to copy the data from one specific range(always the same) and paste in another spreadsheet always in the row below. So basically, it starts pasting on row 11, but if I run again it will paste on the row 12 and there it goes.. The code has been working fine, but there is only one problem. It identifies the next empty row(to paste) based on the value of the column AP, but i want it to identify based on the values of all the columns between AP:BA. Thus, if there is any value on those cells, it should copy on the row below, not only if there is a value on AP. Does someone know how to change my code in order to solve this problem? Thank You very much
Sub Copy_Shanghai()
Dim count As Integer
count = 11
Do While Worksheets("Time Evolution").Range("AP" & count).Value <> ""
'<>"" means "is not empty", as long as this happens we go down looking for empty cell
count = count + 1
Loop
'Now count is row with first empty cell outside of top 10 rows in column C
Worksheets("Fill").Range("E5:P5").Copy
Worksheets("Time Evolution").Range("AP" & count).PasteSpecial xlPasteValues
End Sub