Macro

kbendelac

New Member
Joined
Aug 9, 2020
Messages
36
Office Version
  1. 2019
Platform
  1. Windows
I am trying to modify an existing macro and currently have the following.

WB1.Sheets(1).Cells(j, 13).Formula = "=l" & j & "/K" & j
WB1.Sheets(1).Cells(j, 13).NumberFormat = "0.00%"

When I execute this function it returns the value as #DIV/0! rather than 0.00%

Is there a way to return the value as 0%
Hopefully this makes sense.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
VBA Code:
With WB1.Sheets(1).Cells(j, 13)
    If Range("K" & j).Value = 0 Then
        .Value = 0
        .NumberFormat = "0%"
    Else
        .Formula = "=l" & j & "/K" & j
        .NumberFormat = "0.00%"
    End If
End With

This will put a formula in every row. The formula will automatically adjust for each row.
VBA Code:
WB1.Sheets(1).Range("M2:M" & LastRow).Formula = "=IF(K2=0,0,L2/K2)"
WB1.Sheets(1).Range("M2:M" & LastRow).NumberFormat = "0.00%"
 
Last edited:
Upvote 0
VBA Code:
With WB1.Sheets(1).Cells(j, 13)
    If Range("K" & j).Value = 0 Then
        .Value = 0
        .NumberFormat = "0%"
    Else
        .Formula = "=l" & j & "/K" & j
        .NumberFormat = "0.00%"
    End If
End With

This will put a formula in every row. The formula will automatically adjust for each row.
VBA Code:
WB1.Sheets(1).Range("M2:M" & LastRow).Formula = "=IF(K2=0,0,L2/K2)"
WB1.Sheets(1).Range("M2:M" & LastRow).NumberFormat = "0.00%"
 
Upvote 0
Hi, thanks for your help. For some reason it is still not working. Here is the complete VBA with highlighted areas.

Dim i As Integer 'Scans down Rows Data Sheet
Dim j As Integer 'Prints on New Workbooks
Dim z As String
Dim relativepath As String 'The Directory to save the files to

Dim Count As Integer
Dim Vendor As String

Dim WB As Workbook
Set WB = ActiveWorkbook 'References This Workbook

Dim WS As Worksheet
Set WS = ActiveSheet 'References this Worksheet

Dim WB1 As Workbook 'Assigned to Workbooks being created
Dim WS2 As Worksheet

Count = 0

z = InputBox("Which Month and Year", "Ex 'October-2014'")

'Find Size of List
ListSize = 3
Do Until WS.Cells(ListSize, 1).Value = Empty
ListSize = ListSize + 1
Loop
VendorCount = 0
i = 3
Vendor = WS.Cells(i, 1).Value

Do Until WS.Cells(i, 1).Value = Empty

Vendor = WS.Cells(i, 1).Value
VendorCount = VendorCount + 1
j = 3

'Create the New Workbook
Set WB1 = Workbooks.Add

'Copy OverSheet into NewWorkbook
WS.Copy Before:=WB1.Sheets(1)
WB1.Sheets(1).Name = Vendor

'Trim to Until Vendor Starts
If i > 3 Then
WB1.Sheets(1).Range("A" & j & ":A" & (i - 1)).EntireRow.Delete
End If

'Keep the Vendor Rows and Count
Do Until WB1.Sheets(1).Cells(j, 1) <> Vendor Or _
WB1.Sheets(1).Cells(j, 1) = Empty
j = j + 1
i = i + 1
Count = Count + 1
Loop

'Trim the Rest
WB1.Sheets(1).Range("A" & j & ":A" & ListSize).EntireRow.Delete

'Add Formatting. Start with One Cell then Paste Special the whole row
WB1.Sheets(1).Range("A" & j).Select

Selection.Font.Bold = True
With Selection
.HorizontalAlignment = xlRight
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With

'Paste Special
WB1.Sheets(1).Range("A" & j).Copy
WB1.Sheets(1).Range("A" & j & ":AO" & j).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False

'Merge Grand Totals
WB1.Sheets(1).Range("A" & j & ":F" & j).Merge
WB1.Sheets(1).Range("A" & j & ":F" & j).Value = "Grand Total"
WB1.Sheets(1).Range("A" & j & ":F" & j).HorizontalAlignment = xlCenter

'Add Subtotals
WB1.Sheets(1).Cells(j, 4).Font.Bold = True
WB1.Sheets(1).Cells(j, 4) = "Grand Total"

WB1.Sheets(1).Cells(j, 7).Formula = "=SUM(G3:G" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 8).Formula = "=SUM(H3:H" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 9).Formula = "=SUM(I3:I" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 10).Formula = "=SUM(J3:J" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 11).Formula = "=SUM(K3:K" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 12).Formula = "=SUM(L3:L" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 13).Formula = "=l" & j & "/K" & j
WB1.Sheets(1).Cells(j, 13).NumberFormat = "0.00%"


WB1.Sheets(1).Cells(j, 14).Formula = "=SUM(N3:N" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 15).Formula = "=SUM(O3:O" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 17).Formula = "=SUM(Q3:Q" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 18).Formula = "=SUM(R3:R" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 19).Formula = "=SUM(S3:S" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 20).Formula = "=SUM(T3:T" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 21).Formula = "=SUM(U3:U" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 22).Formula = "=SUM(V3:V" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 23).Formula = "=v" & j & "/U" & j
WB1.Sheets(1).Cells(j, 23).NumberFormat = "0.00%"


WB1.Sheets(1).Cells(j, 24).Formula = "=SUM(x3:x" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 25).Formula = "=SUM(Y3:Y" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 27).Formula = "=SUM(AA3:AA" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 28).Formula = "=SUM(AB3:AB" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 29).Formula = "=SUM(AC3:AC" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 30).Formula = "=SUM(AD3:AD" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 31).Formula = "=SUM(AE3:AE" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 32).Formula = "=SUM(AF3:AF" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 33).Formula = "=AF" & j & "/AE" & j
WB1.Sheets(1).Cells(j, 33).NumberFormat = "0.00%"


WB1.Sheets(1).Cells(j, 34).Formula = "=SUM(AH3:AH" & (j - 1) & ")"
WB1.Sheets(1).Cells(j, 35).Formula = "=SUM(AI3:AI" & (j - 1) & ")"

'Add Subtotals
Application.DisplayAlerts = False
WB1.Sheets(2).Delete
'WB1.Sheets(2).Delete
'WB1.Sheets(2).Delete
Application.DisplayAlerts = True

'Save the New Workbook to Current Directory
relativepath = WB.Path & "\" & "Independent Store Sales Summary - " & Replace(Vendor, ".", "") & " - " & z

WB1.SaveAs Filename:=relativepath

WB1.Close

Loop

MsgBox (VendorCount & " Vendor Files Printed")
MsgBox ((ListSize - 3) & " items on file, " & Count & " items printed")
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,011
Messages
6,122,677
Members
449,092
Latest member
tayo4dgacorbanget

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top