Excel VBA: How to turn formulas into values for the last used column? - vba

I am trying to turn all my formulas into values for the last used column in my sheet. (Last column changes monthly) This is my code so far:
Sub turnvalues2()
Set rng = Range(4, Columns.Count).End(xlToLeft).Column - 1
rng.Value = rng.Value
End Sub

Range(4, Columns.Count).End(xlToLeft).Column - 1 returns a number not a range. You want the Columns() at that number.
Sub turnvalues2()
Set rng = Columns(Cells(4, Columns.Count).End(xlToLeft).Column - 1)
rng.Value = rng.Value
End Sub
Also the - 1 moves it to the second to last column used, You may want to remove that to get the last column used.

Alternate version using Range.Find to get last populated column:
Sub tgr()
Dim ws As Worksheet
Dim rFound As Range
Set ws = ActiveWorkbook.ActiveSheet
Set rFound = ws.Cells.Find("*", ws.Range("A1"), xlFormulas, , xlByColumns, xlPrevious)
If Not rFound Is Nothing Then rFound.EntireColumn.Value = rFound.EntireColumn.Value
End Sub

Related

VBA: Referring to active cells' row in a For/Each loop

the aim of my problem is to find a specific value (Text) and then refer to the entire row (or even better only the used range to the right of my active cell) in a For/Each loop.
The first part works fine of finding my value, however, the code for targeting the row of the active cell (so the cell found by the find function), does not work yet:
Sub Search()
Dim cell As Range
Dim Count As Long
Set cell = Cells.Find(what:="Planned Supply at BP|SL (EA)", LookIn:=xlValues, lookat:=xlWhole)
For Each cell In ActiveCell.EntireRow
If cell.Value = "0" Then
Count = Count + 1
End If
Next cell
Range("I1").Value = Count
End Sub
The following code will find the range to the right of your found cell and use your loop to do the comparision for each cell in the range. That part could probably be improved by using WorksheetFunction.CountIf.
Option Explicit
Sub Search()
Dim wks As Worksheet
Set wks = ActiveSheet
Dim cell As Range, sngCell As Range
Dim Count As Long
Set cell = wks.Cells.Find(what:="Planned Supply at BP|SL (EA)", LookIn:=xlValues, lookat:=xlWhole)
If cell Is Nothing Then Exit Sub ' just stop in case no hit
Dim rg As Range, lastColumn As Long
With wks
lastColumn = .Cells(cell.Row, .Columns.Count).End(xlToLeft).Column ' last used column in cell.row
Set rg = Range(cell, .Cells(cell.Row, lastColumn)) ' used rg right from found cell inlcuding found cell
End With
' loop from the original post
For Each sngCell In rg
If sngCell.Value = "0" Then
Count = Count + 1
End If
Next
Range("I1").Value = Count
End Sub

Return Row Number from Current Selection (Excel VBA)

I am trying to process excel data dumped from quickbooks. In order to do what I want, I need to use first non blank row in column A, which varies depending on the period reported in quickbooks.
I am using
Sub Test()
Selection.End(xlDown).Select
to find the first non blank row in column A.
Say for example I have two files, one where the first non blank cell in column A is A157. Selection.End(xlDown).Select selects A157. I then need to select C1:C157.
The other spreadsheet has the first non blank cell in column A at A122. Selection.End(xlDown).Select selects A122. I then need to select C1:C122. The row in column A found using xlDown is a variable that I then need to use to create a selection in column C.
Any help is much appreciated. Thank you!
To get last row/column in a worksheet try:
Dim ws As Worksheet
set ws = ActiveSheet
With ws
lastRow = ws.Cells.SpecialCells(xlCellTypeLastCell).Row
lastCol = ws.Cells.SpecialCells(xlCellTypeLastCell).Column
End With
To get last row/column in a range try:
Dim rg as Range
With ws
Set rg = .Range(.Cells(1, 3), .Cells(999, 3))
With rg
lastRow = .Cells.SpecialCells(xlCellTypeLastCell).Row
lastCol = .Cells.SpecialCells(xlCellTypeLastCell).Column
End With
End With
To set new range for your last row/column try:
With ws
Set rg = .Range(.Cells(firstRow, firstCol), .Cells(lastRow, lastCol))
End With
If you have more than 1 worksheet you can specify multiple sheets simply like this:
Dim ws(3) As Worksheet
set ws(0) = Worksheets("SheetName1");
set ws(1) = Worksheets("SheetName2");
'etc.
Try,
dim rng as range
set rng = range(cells(1, "C"), cells(activecell.end(xldown).row, "C"))
rng.select

Selecting the last used cell in column A and then extend it to column H

Hi there I am trying to select a range "A2:H2" down to the last filled cell based on column A (so in this case it should select "A2:H59"). The range is not fixed so it cannot be defined with exact numbers. I have the following code, but it selects everything down to the 402nd row even though there is no data beyond "A59" in the sheet. Any idea what is going on? Thanks for the help!
Global ssaw As Worksheet
Global trckr As Worksheet
Sub DataF()
Dim myRange As Range
Dim myCell As Range
Set ssaw = Sheets("SSAW_DATA")
Set trckr = Sheets("SQL_DATA_FEED")
Set myRange = trckr.Range("A2:H2").end(xlDown)
With myRange
.SpecialCells(xlCellTypeBlanks).Interior.Color = RGB(255, 102, 102)
.SpecialCells(xlCellTypeBlanks).Value = "#missing#"
End With
End Sub
If we assume your last used cell in column A is A59 then …
… This
Set myRange = trckr.Range("A2", trckr.Range("A2").End(xlDown))
will select A2:A59 and this
.Resize(ColumnSize:=8)
will resize it to make it 8 columns width that is A2:H59.
So together we get:
Set myRange = trckr.Range("A2", trckr.Range("A2").End(xlDown)).Resize(ColumnSize:=8)
Use this
trckr.Range("A" & trckr.Rows.Count).End(xlUp)
alternatively to find the last used cell in column A if there can be empty cells in between:
Set myRange = trckr.Range("A2", trckr.Range("A" & trckr.Rows.Count).End(xlUp)).Resize(ColumnSize:=8)
exploit the fact that Range(cell1, cell2) is equivalent to Range(cell2, cell1)
Set myRange = trckr.Range("H2", trckr.Range("A2").End(xlDown))
while if you want to select a range from A2:H2 down to column A last not empty cell (i.e. included empty cells along column A in between the first and last not empty ones):
Set myRange = trckr.Range("H2", trckr.Cells(trckr.Rows.Count, 1).End(xlUp))
I would suggest to use the following code
Option Explicit
Function LastRowInColumn(colName As String)
Dim lastRow As Long
With ActiveSheet
lastRow = .Cells(.Rows.Count, colName).End(xlUp).Row
End With
LastRowInColumn = lastRow
End Function
Sub SelectRg()
Dim rg As Range
Dim wks As Worksheet
Dim lastRow As Long
lastRow = LastRowInColumn("A")
Debug.Print lastRow
If lastRow = 1 Then
' do nothing
Else
Set wks = ActiveSheet
With wks
Set rg = Range(.Cells(2, 1), .Cells(lastRow, "H"))
rg.Select
End With
End If
End Sub
The code determins the last filled row in column A and select based on this information everything to column H
EDIT Improved function
Function LastRowInColumn(ByVal wks As Worksheet, ByVal colName As String) As Long
With wks
LastRowInColumn = .Cells(.Rows.Count, colName).End(xlUp).Row
End With
End Function
EDIT2 And if one would not like to use an extra function you could do it like that
Sub SetRg()
Dim rg As Range
Dim wks As Worksheet
Dim lastRow As Long
Set wks = ActiveSheet
With wks
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'lastRow = LastRowInColumn(wks, "A")
If lastRow > 1 Then
Set rg = Range(.Cells(2, 1), .Cells(lastRow, "H"))
End If
End With
End Sub

How can I iterate from a specific Excel cell to the latest row having a value in this column using VBA?

I am absolutly new in Excel and VBA macro and I have the following problem.
I wrote this code that print the content of the K column cell starting from the 3th row to the 5th row:
Dim outQuantityRange As Range
Set outQuantityRange = Range("K3:K5")
For Each currentOutQuantity In outQuantityRange.Cells
MsgBox (currentOutQuantity)
It works fine. My problem is that I want change this code to access to the content of the cell into the K column starting from the 3th row and ending to the last inserted value. For example if the last value is into the K100 cell it have to print the content of: K3, K4, K5,......,K100.
I don't want to specify K100 but it have to stop to the last row having a value into the K column.
How can I implement this behavior?
Next
If there are no gaps between K3 and last row, then this will do the work:
Dim rng As Range
Set rng = Range("K3", Range("K3").End(xlDown))
I've given two ways to find the last cell - using the LastCell function will return the very last cell on the sheet which may not be in column K.
The second way I've shown is just finding the last cell in column K.
The range is then set by giving the first and last cell references separated by a comma.
Sub AllValues()
Dim outQuantityRange As Range
Dim currentOutQuantity As Range
Dim rLastCell As Range
With ThisWorkbook
'Find last cell on sheet containing data.
'Set rLastCell = LastCell(.Worksheets("MySheetName"))
With .Worksheets("MySheetName")
'Find last cell in column K containing data.
Set rLastCell = .Cells(.Rows.Count, 11).End(xlUp)
Set outQuantityRange = .Range("K3", rLastCell)
End With
End With
For Each currentOutQuantity In outQuantityRange
MsgBox currentOutQuantity, vbOKOnly + vbInformation
Next currentOutQuantity
End Sub
Public Function LastCell(wrkSht As Worksheet) As Range
Dim lLastCol As Long, lLastRow As Long
On Error Resume Next
With wrkSht
lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
lLastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
If lLastCol = 0 Then lLastCol = 1
If lLastRow = 0 Then lLastRow = 1
Set LastCell = wrkSht.Cells(lLastRow, lLastCol)
End With
On Error GoTo 0
End Function
If the values in column K are constants, then:
Sub qwerty()
Dim outQuantityRange As Range, zell As Range
Set outQuantityRange = Range("K3:K" & Rows.Count).SpecialCells(2)
For Each zell In outQuantityRange.Cells
MsgBox zell.Value
Next zell
End Sub

Set range to all populated cells

I need to set a range to select only the cells in a row which contain data.
Sometimes there will be data in columns B, C, D, E and F of row 3, whilst at other times there will be data in the first 10 or 20 columns of that row.
I've tried the below, but it doesn't work.
Dim rRng As Range
Set rRng = Sheets(1).Range("B3").End(xltoright)
I know that I'm wide of the mark, but am struggling with this.
This code will be run for a variety of different datasets; sometimes it needs to select five cells, sometimes ten (if populated).
Select to the last column or select non blanks
Sub SelectLstCol()
Dim Col As Long
Dim rng As Range
Col = Cells(3, Columns.Count).End(xlToLeft).Column
Set rng = Range(Cells(3, 2), Cells(3, Col))
rng.Select 'or whatever you want to do with it
End Sub
Sub NonBlanks()
Dim Col As Long
Dim rng As Range
Col = Cells(3, Columns.Count).End(xlToLeft).Column
Set rng = Range(Cells(3, 2), Cells(3, Col)).SpecialCells(xlCellTypeConstants, 23)
rng.Select 'or whatever you want to do with it
End Sub
Using a function to be able to reuse it more easily :
Sub test_andrew_abbott()
MsgBox Get_Row_Range(5).Address
MsgBox Get_Row_Range(Range("A12").Row).Address
End Sub
This will be more robust than with .End(xlToRight) :
Public Function Get_Row_Range(RowNumber As Long) As Range
Dim wS As Worksheet, _
rRng As Range
Set wS = Sheets(1)
With wS
Set rRng = .Range(.Range("A" & RowNumber), _
.Cells(RowNumber, .Columns.Count).End(xlToLeft))
End With
Set Get_Row_Range = rRng
End Function
You can connect to your excel file as a data set, I find it better than other way