VBA - If/else not working

Doedtman

Board Regular
Joined
May 21, 2010
Messages
92
Hello!
I'm running into a problem, as I run this code. It runs without any errors, but on the If formula, the cells are not clearing out. I've colored the line I think may be the problem. I'd really appreciate any help with figuring out what I'm doing wrong. Thanks!

Rich (BB code):
Sub QtrInfo()
Dim strMainWorkbook As String
Dim strMainWorksheet As String
Dim strDetailWorkbook As String
Dim a As Integer
Dim x As Long
 
strMainWorkbook = ActiveWorkbook.Name
Sheets("Input").Activate
 
' set a as the segment code (tab names)
a = 5
strMainWorksheet = Range("A" & a).Value
strDetailWorkbook = Range("A3").Value
 
'Open File to get current quarter data
Workbooks.Open Filename:="C:\Documents and Settings\joddoe\Desktop\" & strDetailWorkbook, ReadOnly:=True
 
'Insert the correct dates into each tab A11:A22
 
Workbooks(strDetailWorkbook).Activate
Sheets("Rev").Range("B1").Copy
Workbooks(strMainWorkbook).Activate
Sheets(strMainWorksheet).Range("A11").PasteSpecial xlPasteValues
 
 
Workbooks(strDetailWorkbook).Activate
Sheets("Rev").Range("C1").Copy
Workbooks(strMainWorkbook).Activate
Sheets(strMainWorksheet).Range("A12").PasteSpecial xlPasteValues
 
 
 Workbooks(strDetailWorkbook).Activate
Sheets("Rev").Range("D1").Copy
Workbooks(strMainWorkbook).Activate
Sheets(strMainWorksheet).Range("A13").PasteSpecial xlPasteValues
 
'If x is positive, clear our cells "B11:D22"
 
x = Sheets(strMainWorksheet).Range("D29").Value
If x > 0 Then
    Range("B11:D23").ClearContents
'if x is not positive, move data up 3 rows and clear ("B23:D23")    
Else
    Range("B14:D22").Copy
    Range("B11").PasteSpecial xlPasteValues
    Range("B23:D23").ClearContents
End If
Workbooks(strDetailWorkbook).Close
 
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
It looks OK but you may want to add the sheet reference to the line that clears the cells so you're sure it's clearing the cells on the right sheet.
 
Upvote 0

Forum statistics

Threads
1,225,071
Messages
6,182,685
Members
453,132
Latest member
nsnodgrass73

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