How to delete blanks after a formula - excel

I'm trying to create a Match macro that compares two lists and gives me the cells that present in only one of the lists. The cells are then copied to another sheet where the cells are counted. However, the blank cells are also being copied and I don't know why.
The below is what I have:
Sub Macro_do_Match()
Dim CopyrangeB As String
Dim lRowB As Integer
Dim fRowB As Integer
Dim CopyrangeD As String
Dim lRowD As Integer
Dim fRowD As Integer
Dim rng As Range
' Defines range for column B
lRowB = Cells(Rows.Count, 1).End(xlUp).Row
fRowB = 2
Let CopyrangeB = "B" & fRowB & ":" & "B" & lRowB
' "macro"
Range("B2").Select
ActiveCell.FormulaR1C1 = _
"=IF(NOT(ISERROR(MATCH(C[-1],C[1],0)))=FALSE,C[-1], """")"
Range("B2").Select
Selection.AutoFill Destination:=Range(CopyrangeB)
' Defines range for column D
lRowD = Cells(Rows.Count, 3).End(xlUp).Row
fRowD = 2
Let CopyrangeD = "D" & fRowD & ":" & "D" & lRowD
' "macro"
Range("D2").Select
ActiveCell.FormulaR1C1 = _
"=IF(NOT(ISERROR(MATCH(C[-1],C[-3],0)))=FALSE,C[-1], """")"
Range("D2").Select
Selection.AutoFill Destination:=Range(CopyrangeD)
'Copy and paste B
Range("B2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Final Results").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Copy and paste D
Sheets("Insert Lists").Select
Range("D2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Final Results").Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

Related

Vlookup run-time error for dynamic range and another worksheet

I am getting run-time error for Vlook-up formula while using dynamically from activesheet to another sheet i.e Binarysheet, in Binary sheet, my lookup range is A to C column
I need to iterate the for loop and I have to use vlookup inside the for loop
Dim wfd As Worksheet
Set wfd = thisworkbook.Sheets("Binarysheet")
For i = 20 To 61
Cells(2, i).Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],wfd!$A:$B,2,False)"
ActiveCell.Offset(0, -1).Range("A1").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlUp).Select
ActiveCell.Offset(0, 1).Range("A1").Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Cells(2, i+1).Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-3],wfd!$A:$C,3,0)"
ActiveCell.Offset(0, -1).Range("A1").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlUp).Select
ActiveCell.Offset(0, 1).Range("A1").Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next
With the above comments and advice from the two linked questions, we can simplify the whole thing and load the whole without the need to loop:
Dim wfd As Worksheet
Set wfd = ThisWorkbook.Sheets("Binarysheet")
With ActiveSheet
Dim lastrow As Long
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
Dim rng As Range
Set rng = .Range(.Cells(2, 20), .Cells(lastrow, 61))
rng.FormulaR1C1 = "=VLOOKUP(RC1,'" & wfd.Name & "'!C1:C65,COLUMN(RC),False)"
rng.Value = rng.Value
End With

How do I create a macro that will run a formula loan by loan, and paste the output in a separate sheet

I'm setting up a pricing model and am wondering how I am able to get the macro to run the pricing loan by loan and have the output pasted in a separate tab (this would also be loan by loan, so it cannot overwrite). I used the macro recorder and this is what I have so far, but I'm a novice and not sure how to loop this until it hits a blank cell (I did the first two loans....)
Sub Macro1()
'
' Macro1 Macro
'
'
Selection.Copy
Sheets("Cashflows").Select
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Output").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Input").Select
Range("A3").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Cashflows").Select
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Output").Select
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
The tools you need:
To figure out the last row:
Dim LastRow As Integer
LastRow = Range("A" & Rows.Count).End(xlUp).Row
'This simulates selecting the last cell in "A" Column,
'hitting "End" and "Up Arrow", then returns that row number
'as in integer.
To cycle through each row:
Dim I As Integer
For I = 1 To 10 '(Or replace "10" with "LastRow")
'Do something like look at a range value:
Debug.Print Cells(I, 1).Value
Next I
Finally, this is going to be a lot easier if you use .value = .value instead of copying and pasting:
Dim RowNum As Integer
RowNum = 10
Range("A1").Value = Range("B1").Value 'Copies Value from B1 into A1
Cells(1, 1).Value = Range("B1").Value 'Does Exact same thing as above: Cells(row, column)
'Copy A10:C10 from sheet2 to sheet1:
Sheet1.Range("A" & RowNum & ":C" & RowNum).Value = Sheet2.Range("A" & RowNum & ":C" & RowNum)
See how far you get with that and come back if you have more specific questions.
There are lots of good resources out there if you're having trouble.

VBA changes date format

I have a table with data in it and run a macro to neaten things up and then adds a hyperlink to column G but the issue is when the macro has run, the date changes from:
https://websitenamehere.com//agentView/agentname#company/2021-11-08
to
https://websitenamehere.com//agentView/agentname#company/44508
In my table, I created in column H the column for "today" and then in column G is where it puts it all together but messing up the date part.
Here is my code which i am using. Any help would be appreciated.
Sub CleanFollowUps()
Dim Lrow As Integer
Dim lCol As Integer
Dim C As Range
Lrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
lCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
ActiveSheet.ListObjects.Add(xlSrcRange, Range(Cells(1, 1), Cells(Lrow, lCol)), , xlYes).Name = "FollowUps"
Range("FollowUps[#All]").Select
ActiveSheet.ListObjects("FollowUps").TableStyle = ""
Range("F1").Select
ActiveCell.FormulaR1C1 = "Helper"
Range("F2").Select
ActiveCell.FormulaR1C1 = "=IF(SUM([#[Due today]]+[#Late])>0,""Yes"","""")"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Schedule"
Range("H1").Select
ActiveCell.FormulaR1C1 = "day"
Range("H2").Select
ActiveCell.FormulaR1C1 = "=TODAY()"
Columns("H:H").Select
Selection.NumberFormat = "yyyy-mm-dd"
Range("H2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("G2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = _
"=""https://websitenamehere.com/agentView/""&[#Username]&""#company/""&[#day]"
Range("G2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
With Sheets("FU")
For Each C In .Range("G2:G" & .Range("G" & .Rows.Count).End(xlUp).Row)
.Hyperlinks.Add Anchor:=C, Address:=C.Value, SubAddress:=C.Value
Next C
End With
Application.DisplayAlerts = True
End Sub

Selection.AutoFill Destination:=Range(strRange) causes Runtimeerror `1004` The AutoFill-method of the range object coudnt be run

Dim row As Integer
Dim strRange As String
Selecting the Sheet
Sheets("Matrix").Select
Range("C3").Select
ActiveCell.FormulaR1C1 = _ "=SUMIF(Aufstellung!R13C2:R[997]C,Matrix!RC[-1],Aufstellung!R13C3:R[997]C)"
Range("C3").Select
row = Count() + 3 - 1
strRange = "C3:C" & row
Range(strRange).Select
Selection.AutoFill Destination:=Range(strRange), Type:=xlFillDefault
---> here comes the Runtimeerror 1004: The AutoFill-method of the range object coudnt be run
strRange = "B3:D" & row
Range(strRange).Select
Selection.Copy
strRange = "F3:H" & row
Range(strRange).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Kassenblatt").Select
row = Count() + 6 - 1
strRange = "C6:F" & row
Range(strRange).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B6").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=IF(RC[4]>0,R[-1]C+1,"""")"
Range("B6").Select
strRange = "B6:B" & row
---> here will probably come the same Runtimeerror 1004: The AutoFill-method of the range object coudnt be run
Selection.AutoFill Destination:=Range(strRange), Type:=xlFillDefault
Range(strRange).Select
Range("A6").Select
ActiveCell.FormulaR1C1 = "=IF(RC[5]>0,R1C4,"""")"
Range("A6").Select
strRange = "A6:A" & row
---> here will probably come the same Runtimeerror 1004: The AutoFill-method of the range object coudnt be run
Selection.AutoFill Destination:=Range(strRange), Type:=xlFillDefault
End Sub
Helper Function for counting all rows, that have a value - can be ignored for the question
Public Function Count() As Integer
Dim n As Integer
n = Worksheets("Formeln & Daten").Range("G21:G1000").Cells.SpecialCells(xlCellTypeConstants).Count
Count = n
End Function
Try this:
Sheets("Matrix").Range(strRange) = "=" & "SUMIF(Aufstellung!R13C2:R[997]C,Matrix!RC[-1],Aufstellung!R13C3:R[997]C)"

I need to insert a total at the bottom of a dynamic table in Excel VBA

I am having some issues with my VBA code that will select data from one sheet, copy it, paste it to a new sheet and insert a total at the bottom of the table. The first steps work, but I am struggling with the total , any help would be greatly appreciated. Here is what I have so far:
Range("A3").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets.Add After:=ActiveSheet
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Cells.Select
Cells.EntireColumn.AutoFit
Range("A1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "='Pricing Main'!RC[1]"
Range(xlToRight, xlDown).Offset(1, 0).Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=SUM(R[-36]C:R[-1]C)"
End Sub
If the problem is in making formula with dynamic range (while first row is always the same and the 2nd is different), you could use:
ActiveCell.FormulaR1C1 = "=SUM(R1C:R[-1]C)"
If you're using number without "[]" it will absolute address with $
In this example it will be ="SUM(A1:Ax)" where X is row before active cell
Another option is using Activecell.Formula and get this address by combining letters with Activecell.row
activecell.Formula = "=SUM(A1:A" & activecell.Row - 1 &")"
And after that you can autofill to other columns if needed by
activecell.AutoFill Destination:=range(activecell, activecell.Offset(0,5)), Type:=xlfilldefault
3rd option could be using range variables calculate address before formula:
Sub Makro1()
Dim rng1 As Range Dim rng2 As Range Dim rng3 As Range
Set rng1 = Range("A1") Set rng2 = ActiveCell.Offset(-1, 0) Set rng3 = Range(rng1, rng2)
ActiveCell.Formula = "=SUM(" & rng3.Address & ")"
End Sub
If you have another problem, please, try explain it better ;)

Resources