how to get unlimited range in vba - excel

Dim Brand As String
Worksheets("1st").Select
Brand = Range()
Hi, I am using VBA coding in microsoft excel 2011 and I am a little stuck with range. Lets say I want to start something in cell c4, I enter in c4 in the range but if I then want it to continue till all the way down the column (unlimited as we don't know how many rows in that column will be filled up), then what is the correct way of writing this?

Range("C4", Range("C4").End(xlDown))
This will return the range, C4 to C4 and Down.
This will select all contiguous cell from C4 down all the way until the first blank cell.
Now if you Data has a chance that it might have blanks in between non blank cells. Then you can use the following:
Range("C4", Range("C" & Rows.Count).End(xlUp))
this will select C4 to The last cell in C columns up until the the cell with data. In other words this will select EVERY cell in the C column from C4 until the last cell in the entire column with a value, and include blanks.

Another way
Sub Main()
Dim Brand As String
Dim lastRow As Long
Dim rng As Range, cell As Range
Dim sht As Worksheet
Set sht = ThisWorkbook.Worksheets("1st")
With sht
lastRow = .Range("C" & .Rows.Count).End(xlUp).Row
End With
If lastRow < 4 Then lastRow = 4
Set rng = sht.Range("C4:C" & lastRow)
For Each cell In rng
MsgBox cell.Value
Next
End Sub

Related

Trying to reference and pull all cells that have a blank in another column

I am brand new to VBA as a disclaimer.
I have data I am inputting into one sheet (Emptys) and am trying to grab a cell from another sheet (report) in the same row that has a blank in a certain column.
For example:
Sample Report
I am trying to pull the value from the column A if the cell in the same row of column M is empty and reference that to a different sheet.
I am also trying to skip any rows that have a value in them and only pull the data from the rows that have a blank in column M.
I have tried a few things and I am in a bit over my head.
All that I have gotten to work is this basic formula:
=IF( Report!M2= "",Report!A2, "" )
I still have to sort out empties manually this way.
I feel like I was on the right track here but not sure where I went wrong:
Dim myrange
Dim id
myrange = Sheets("Report").Range("M2:M")
id = Sheets("Empty_Slots").Range("A2:A")
For Each cell In myrange
If IsEmpty(cell) Then
id = Sheets("Report").Range("A2:A")
End If
Next cell
Any help and explanation would be greatly appreciated!
You may access row numbers of those blank cells and use them in worksheet 2:
Sub Macro1()
Dim wk1 As Worksheet
Dim wk2 As Worksheet
Dim LR As Long
Dim rng As Range
Set wk1 = Worksheets("Sheet1")
Set wk2 = Worksheets("Sheet2")
With wk1
LR = .Range("A" & .Rows.Count).End(xlUp).Row 'last non blank cell in column a
For Each rng In .Range("M2:M" & LR).SpecialCells(xlCellTypeBlanks) 'array of blank cells in column M
wk2.Range("A" & rng.Row).Interior.Color = vbYellow 'do something with column A from worksheet2 based on row numbers from column M in worksheet 1
Next rng
End With
Set wk1 = Nothing
Set wk2 = Nothing
End Sub
The code loops trough each blank cell in column M from Sheet1 and pulls the row number. Then in worksheet 2 it uses that row number in column A.
After executing code, I've colored cells in sheet2 but based on sheet1

Copy Formula to last cell in active row range then copy all the way to last column - VBA

I am trying to copy a formula from C2 down to the last cell in the active row range (columns A and B count) then copy that same formula to every column in the active column range (row 1 count)
IE copy formula all the way down then all the way across....
Can you dim both last row and last column in the same Subroutine?
If so, how do I do this? I tried with two subroutines and failed miserably.
Sub ImportSub2()
Dim LastRowColumnA As Long
LastRowColumnA = Cells(Rows.Count, 1).End(xlUp).Row
Range("C2:C" & LastRowColumnA).Formula = "=$A2&C$1"
Call ImportSub3
End Sub
Sub ImportSub3()
Dim lastcolumn As Long
lastcolumn = Cells(Columns.Count, 1).End(xlUp).Column
Range("C2:C" & lastcolumn).Formula = "=$A2&C$1"
End Sub
Yes, this can be done; your first problem above is that you are not finding the last column correctly.
VBA formulas, especially with a mix of relative and absolute references, work better with R1C1 format. This code directly writes the required formula into the cells of interest:
Sub InjectFormula()
Dim LastRow As Long, LastCol As Long
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
Range(Range("C2"), Cells(LastRow, LastCol)).FormulaR1C1 = "=RC1&R1C"
End Sub
If you want to copy a pre-existing formula from C2, you can instead use
Range(Range("C2"), Cells(LastRow, LastCol)).FormulaR1C1 = Range("C2").FormulaR1C1

Find text in one column, if true then match the text in the column next to it with a column on a separate sheet and insert a formula

Here's what I have:
Response Flow
I have one sheet called Response Flow that has Response, Y/N and a Total. If the Response has a Y next to it I want to match the Response Name with the Response Name on Sheet 2 ("Campaigns") and insert a formula in the column next to the response name on Sheet 2 using VBA code. Below is what I have so far.
Sub Volume_Calc()
Dim LastRowR As Long
Dim LastRowC As Long
Dim LastRowI As Long
Dim LastRowA As Long
Dim rngFoundCell As Range
Dim cell As Range
Dim text As String
Dim FindRow As Range
LastRowR = Range("C65536").End(xlUp).Row
LastRowC = Range("K65536").End(xlUp).Row
LastRowI = Range("I65536").End(xlUp).Row
LastRowA = Range("A65536").End(xlUp).Row
Set FindRow = Worksheets("ResponseFlow").Range("C:C").Find(What:="Y",
LookIn:=xlValues)
Do While FindRow = True
If Application.Match(Worksheets("Campaigns").Range("K6"),
Worksheets("ResponseFlow").Range("A4:A" & LastRowA), 0) Then
Worksheets("Campaigns").Range("I6:I" & LastRowI).Formula = "=INDEX(ResponseFlow!$B$3:$B$145,MATCH(Campaigns!$K6,ResponseFlow!$A$3:$A$145,0))"
End If
Loop
End Sub
What you're intending to do seems like it'd be easier to do in Excel without VBA, but if you insist on having some macro insert formulas, this might be an easy approach. First put the dynamic formula you want to be pasting in to the right of the columns with a Y/N, SOMEWHERE in your sheet. In my example below I used Cell("Z1"). Make sure it's dynamic so that if you were to copy/paste formula into another cell, it would adjust correctly.
Again make sure whatever dynamic match formula you want to the right of your values is somewhere and configured to be dynamic. In my example it's on Response ws in cell Z1.
Sub Volume_Calc()
Dim Resp_WS As Worksheet: Set Resp_WS = Worksheets("ResponseFlow")
Dim CAMP_WS As Worksheet: Set CAMP_WS = Worksheets("Campaigns")
Dim rCell As Range
Dim cCell As Range
'Loops through Response Sheeet column "C" looking for values of "Y"
For Each rCell In Intersect(Resp_WS.Range("C:C"), WResp_WS.UsedRange).Cells
If UCase(rCell.Value) = "Y" Then
'When finds a cell with Y, it then loops through Campaigns Sheet column "I"
'looking for a value that matches one column to the left where the "Y" was found
For Each cCell In Intersect(CAMP_WS.UsedRange, CAMP_WS.Range("I:I")).Cells
'When match is found, the macro will insert the formula to the right
'of the cell in Campaigns, with the dynamically updated formula in cell Z1
If cCell.Value = rCell.offset(0,-1).Value Then
cCell.Offset(0, 1).FormulaR1C1 = Resp_WS.Range("Z1").FormulaR1C1
End If
Next cCell
End If
Next rCell
End Sub

Excel VBA - If number of rows contain text then enter specific text in cell

I am trying to work out how to count number of cells that have text in them and then depending on the total number of cells that have text, paste certain text in other cells.
i.e If cell A1:A3 contain text, then enter "3x1" in cell B1 and enter "3x2" in cell B2.
likewise, If cell A1:A4 contain text, then enter "4x1" in cell B1 and enter "4x2" in cell B2.
UPDATE1:
Let me try to give another example since numbers confuses people when it comes to coding :) IF cells A1:A3 have text, count it and then depending on how many number of cells have text, enter below in B2 and B3 2 cells with text = "Three" in B2 and text "Four" in B3 3 cells with text = "Five" in B2 and text "Six" in B3
Including above, i would like to add one more thing which is to ignore any cell and dont include it in the count if it has, lets say digits 242
Please look into the solution for your requirement
Sub test1()
Dim wb As Workbook
Dim rng As Range
Dim LastRow As Long
Dim ws As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet1")
With ws
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
ws.Range("B1").Value = """" & LastRow & "X1"""
ws.Range("B2").Value = """" & LastRow & "X2"""
End Sub
Example like this.
Sub Testing()
LastRow = Range("A" & Rows.Count).End(xlUp).Row`
For i = 1 to LastRow
If Worksheets(ActiveSheet.Name).Cells(j, i) = "Pass" Then
Worksheets(ActiveSheet.Name).Cells(k, i).Value = "Pass"
End If
Next i

Excel macro copy right to next cell

Need help here with a macro..
We have at column A all the data starting from A2. What we want to do is create a loop that if column A has value will copy A2 to B2, A3 to B3 and so on. A copy - paste macro wont help us because we filter the data of column A at our existing macro and if we copy and paste it at column B it will not paste the value right next to it.
So we want a loop that scans all column A, finds the non empty and when it finds a value paste it right to the next field. For example A335 to B335 and when it goes to the end of A to stop.
Thank you in advance!
Try this code:
Sub CopyToRight()
Dim rng As Range
Dim LastRow As Long
Dim cell As Variant
LastRow = ActiveSheet.Cells(.Rows.Count, "A").End(xlUp).Row
Set rng = Range("A2:A" & LastRow)
For Each cell In rng
If cell.Value <> "" Then
cell.Offset(0, 1).Value = cell.Value
End If
Next cell
End Sub

Resources