copy cells to different worksheet based on worksheet name - excel

I have been looking around and i cant seem to find the same flavor of question that I have
lets say I have a worksheet that has two columns of data one column I apply a filter too and then use that to create worksheets. I want then to paste the values from the second column that to a matching worksheet
So if I had column a had values a b c c d
and column b had values name fluid id state county
I want the code to look at name and paste it to the proper worksheet in this example it would be worksheet a for fluid it would be worksheet b and so on and so forth
i am thinking its a for each formula and then loop it so
Sub C2s()
Dim contr As Control
Dim RN As Range
Dim LR As Integer
LR = Sheet3.Range("A" & Rows.Count).End(xlUp).Row
RN = Sheet3.Range("B1:B" & LR)
For Each Cell In RN
If
End If
Next
End Sub

** Edited to fix column range being allocated
This is untested, but should give you the idea of how to loop and use the offset column to process your values. The SpecialCells(xlCellTypeVisible) means it will only process the filtered records in your selection
Dim MyCell As Range,
Dim MyRange As Range
Dim WorksheetToCopyTo as string
Dim LR As Integer
LR = Sheet3.Range("A" & Rows.Count).End(xlUp).Row
Set MyRange = Sheet3.Range("A1:A" & LR)
For Each MyCell In MyRange.SpecialCells(xlCellTypeVisible)
WorksheetToCopyTo = MyCell.offset(0,1)
Select Case WorksheetToCopyTo
Case "Fluid"
<do something here>
Case "County"
<do something here>
End Select
Next MyCell

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

How to find the first empty cell in a column, enter a value, and copy that value down to the last row in a table?

Ultimately, I'm having a hard time piecing together the code I've researched into something that works.
I am trying to enter "1PAE" into the first empty cell in a column. Then copy the value down the rest of the column. I'm using Column A to identify the last row in the table.
I believe the 2nd example code shown below is the closest I've gotten to something that works. I've tried to use different versions of the standard FillDown approach.
The 1st example is a version of the code from Scott Holtzman
Copy and Paste Row and Columns(with formulas) and Paste down to the last row in VBA
I've switched my focus to the code below. I'm still getting an error.
Run-time error 1004 - Error 1004 Method 'Range' of object '_Worksheet' failed on the last line...LastRow.AutoFill(Destination:=.Range(LastRow), Type:=xlFillValues) = "1PAE"
Sub PAECombineBillDetail()
Dim Colm As Range
Dim Col1 As Variant, Col2 As Variant, Col3 As Variant, Col4 As Variant, Col5 As Variant, Col6 As Variant
Dim Col7 As Variant, Col8 As Variant, Col9 As Variant, ColA As Variant
Dim OrgFirstBlank As Long
Dim ColLast As Long
Dim pRange As Range
Dim lRow As Long
Dim LastRow As Range
Dim LegBill As Worksheet
Dim BIL019 As Worksheet
Dim INVHIST As Worksheet
'~~> Set Worksheet Names
Set INVHIST = ThisWorkbook.Sheets("CombinedINVHistory")
Set LegBill = ThisWorkbook.Sheets("BillingInvHistory")
Set BIL019 = ThisWorkbook.Sheets("BIL-019")
INVHIST.Select
Set LastRow = Range("B5").End(xlDown).Offset(1, 0)
With INVHIST
'~~> Find Last Row in Col B
lRow = .Range("B" & .Rows.Count).End(xlUp).Row
'~~> FillDown Formula Lvl3 Acct Name
.Range("O6:O" & lRow).Formula _
= "=IF(RC[-3]=""Labor"",INDEX(LBR_CPP5_AcctMap!C1:C15,MATCH(RC[1],LBR_CPP5_AcctMap!C7,0),6),INDEX(NonLBR_CPP5_AcctMap!C1:C12,MATCH(RC[1],NonLBR_CPP5_AcctMap!C7,0),5))"
'~~> FillDown Formula Lvl3 Acct ID
.Range("N6:N" & lRow).Formula _
= "=IF(RC[-2]=""Labor"",INDEX(LBR_CPP5_AcctMap!C1:C15,MATCH(RC[2],LBR_CPP5_AcctMap!C7,0),5),INDEX(NonLBR_CPP5_AcctMap!C1:C12,MATCH(RC[2],NonLBR_CPP5_AcctMap!C7,0),4))"
'~~> FillDown Formula Lvl2 Acct ID
.Range("M6:M" & lRow).Formula _
= "=IF(RC[-1]=""Labor"",INDEX(LBR_CPP5_AcctMap!C1:C15,MATCH(RC[3],LBR_CPP5_AcctMap!C7,0),3),INDEX(NonLBR_CPP5_AcctMap!C1:C12,MATCH(RC[3],NonLBR_CPP5_AcctMap!C7,0),2))"
'~~> Select LastRow to first blank cell in column B
LastRow.Select
'~~> Enter "1PAE" into LastRow and Autofill down the rest of the column.
LastRow.AutoFill(Destination:=.Range(LastRow), Type:=xlFillValues) = "1PAE"
End With
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
In the end, I need "1PAE" in every cell down the remainder of Column B, while keeping everything above the first blank row untouched.

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

how to get unlimited range in vba

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

Excel VBA - select a dynamic cell range

I want to be able to dynamically select a range of cells (the heading row), where the row is 1 but the columns with be for 1 to last column, where "A" is the first Column and where "M" is the last column. I know how to find the last column, but I don't know how to modified the below range to input the first and last column as "A" and "M".
Range("A1:M1").Select
If you want to select a variable range containing all headers cells:
Dim sht as WorkSheet
Set sht = This Workbook.Sheets("Data")
'Range(Cells(1,1),Cells(1,Columns.Count).End(xlToLeft)).Select '<<< NOT ROBUST
sht.Range(sht.Cells(1,1),sht.Cells(1,Columns.Count).End(xlToLeft)).Select
...as long as there's no other content on that row.
EDIT: updated to stress that when using Range(Cells(...), Cells(...)) it's good practice to qualify both Range and Cells with a worksheet reference.
sub selectVar ()
dim x,y as integer
let srange = "A" & x & ":" & "m" & y
range(srange).select
end sub
I think this is the simplest way.
So it depends on how you want to pick the incrementer, but this should work:
Range("A1:" & Cells(1, i).Address).Select
Where i is the variable that represents the column you want to select (1=A, 2=B, etc.). Do you want to do this by column letter instead? We can adjust if so :)
If you want the beginning to be dynamic as well, you can try this:
Sub SelectCols()
Dim Col1 As Integer
Dim Col2 As Integer
Col1 = 2
Col2 = 4
Range(Cells(1, Col1), Cells(1, Col2)).Select
End Sub
I like to used this method the most, it will auto select the first column to the last column being used. However, if the last cell in the first row or the last cell in the first column are empty, this code will not calculate properly. Check the link for other methods to dynamically select cell range.
Sub DynamicRange()
'Best used when first column has value on last row and first row has a value in the last column
Dim sht As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range
Set sht = Worksheets("Sheet1")
Set StartCell = Range("A1")
'Find Last Row and Column
LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column
'Select Range
sht.Range(StartCell, sht.Cells(LastRow, LastColumn)).Select
End Sub

Resources