I have a macro programme to do some operations like Vlookup, deleting columns etc. The file to be updated would have a few worksheets in it and every time the worksheets' name and order may be different. Thus, I would like to be able to choose the worksheet I want every time I use the macro. However, I haven't be successful.....
This is the macro. I wanted the mySheet to be variable. Ideally, It can prompt me to choose the worksheet I want within that wbSource..However, I have been getting errors. Anyone know how do I do it?
Thanks in advance!
Sub Macro1()
Dim file1 As String
Dim file2 As String
Dim wbSource As Workbook
Dim wbLookup As Workbook
Dim startRange As Range
Dim mySheet As Worksheet
Dim col As Range
Dim Del As Range
file1 = Application.GetOpenFilename(Title:="Select the file to update")
If Len(Dir(file1)) = 0 Then Exit Sub
file2 = Application.GetOpenFilename(Title:="Select the LOOKUP file")
If Len(Dir(file2)) = 0 Then Exit Sub
Set wbLookup = Workbooks.Open(file2)
Set wbSource = Workbooks.Open(file1)
Set mySheet = wbSource.Sheets(ActiveSheet.Name)
On Error Resume Next
Application.DisplayAlerts = False
Set col = Application.InputBox _
(Prompt:="Select Column.", _
Title:="Where do you want to insert the columns?", Type:=8)
On Error GoTo 0
Application.DisplayAlerts = True
col.Resize(, 5).EntireColumn.Insert
On Error Resume Next
Application.DisplayAlerts = False
Set Del = Application.InputBox _
(Prompt:="Select Column.", _
Title:="Which column to delimit?", Type:=8)
On Error GoTo 0
Application.DisplayAlerts = True
Del.EntireColumn.Select '** ERROR HERE!!
Selection.TextToColumns _
Destination:=Del, _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, _
Semicolon:=False, _
Comma:=False, _
Space:=False, _
Other:=True, _
OtherChar:="-"
Del.Offset(0, 2).Delete
Del.Offset(0, 1).Delete
On Error Resume Next
Set startRange = Application.InputBox("Select the first cell for the formula", "Autofill VLOOKUP", Type:=8)
On Error GoTo 0
If Not startRange Is Nothing Then
Application.Goto startRange
startRange.FormulaR1C1 = "=VLOOKUP('[" & wbSource.Name & "]" & mySheet.Name & "'!RC[-1],'[" & wbLookup.Name & "]NON SLL'!C1:C3,3,FALSE)"
End If
End Sub
Some continuation from comments and possible explanation of problems...
Your code isn't making any errors. However, there could be some logic mistakes which you should consider. This line of code:
Set wbSource = Workbooks.Open(file1)
activate the workbook just opened (file1). And next line:
Set mySheet = wbSource.Sheets(ActiveSheet.Name)
set variable to sheet in just opened workbook (file1) and sheet which is currently active there.
And general question- is this correct for the logic of further part of your code?
Related
Private Sub Updateform()
Dim WB As Workbook
Dim URL As Variant
Dim Sh As Worksheet
Dim WB1 As Workbook
Dim i As Integer
Dim LR As Long
Dim stext As String
Dim stext1 As String
Application.DisplayAlerts = False
ThisWorkbook.Activate
Set WB = ThisWorkbook
Application.DisplayAlerts = False
ThisWorkbook.Activate
Set Sh = ThisWorkbook.Sheets("Database")
irow = ThisWorkbook.Sheets("Database").[Counta(Database!A:A)] + 1
With Sh
ThisWorkbook.Sheets("Emailtosend").Range("A1999").Value = FirstForm2.UD1.Value
stext = ThisWorkbook.Sheets("Emailtosend").Range("A1999").Value
End With
ThisWorkbook.Sheets("Database").Select
Range("A1").Select
On Error Resume Next
ActiveCell.Select
Dim UID As String
UID = stext
For i = 2 To irow
If ThisWorkbook.Worksheets("Database").Cells(i, 10).Value = UID Then
Worksheets("Database").Cells(i, 3).Value = FirstForm2.lstprocessingdate.Value 'iam able to update date
Worksheets("Database").Cells(i, 4).Value = FirstForm2.lstprocessed1.Value
Worksheets("Database").Cells(i, 8).Value = FirstForm2.survey1.Value ' this is a combobox though I change new value it is still not getting updated
Worksheets("Database").Cells(i, 6).Value= FirstForm2.lstcomments.Value ' this is comment box still the new comments are not getting updated
End If
Next
Dim ncell As Range
For Each ncell In Sheets("temp").Range("Checkrange")
With Sh
If FirstForm2.Controls(ncell.Value) = "" Then
MsgBox ("Make sure all text boxes have entries")
Exit Sub
Else
End If
End With
Next ncell
URL = "https://audit.global.com/sites/AdminSS/Shared%20Documents/Training%20Materials/SS%20recurring%20request%20Handbooks/Test/Updated%20Quality%20Tracker.xlsx?d=w68cd37bd0505426fb4d6fe38c21e23a8"
Set WB1 = Workbooks.Open(URL)
Application.Visible = False
Debug.Print WB1.FullName
Set WB1 = ActiveWorkbook
WB1.LockServerFile
If Err.Number <> 0 Then
MsgBox "File is already open, request you to wait for 10 minutes!"
GoTo 0
Err.Clear
Else
MsgBox "The form is getting updated"
End If
Dim rng1 As Variant
WB1.Activate
Range("J1").EntireColumn.Select
Selection.Copy
Range("K1").EntireColumn.Select
Selection.PasteSpecial xlPasteValues
Dim stext2 As String
stext2 = ThisWorkbook.Sheets("Emailtosend").Range("A1999").Value
WB.Activate
WB.Sheets("Database").Range("A1").Select
'If WB1.Worksheets("Database1").Cells(i, 10).Value = UID Then
Set rng1 = Cells.Find(What:=stext2, _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
ActiveCell.Select
ActiveCell.entirerow.Select
Selection.Copy ' copying the entirerow and I want to paste this data in sheet from sharepoint
WB1.Activate
WB1.Sheets("Database1").Select
Set rng1 = Cells.Find(What:=stext2, _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
ActiveCell.Select
ActiveCell.entirerow.Select
Selection.PasteSpecial xlPasteValues ' I want to paste here after searching the text value
WB1.Save
WB1.Close ' to close sharepoint excel
msgvalue = MsgBox("The information has been updated", vbOKOnly)
0:
Application.Visible = False
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.Visible = False
End Sub
I want to update existing macro sheet by calling existing details from userform unique ID and then update new details entered in the existing sheet and other tracker in Sharepoint. Please help
Let me know if you have any questions. Not sure what else to add to this question. Initially the code has to modify the existing details and update the new details and then open Sharepoint Excel and check the unique code, either delete the entire row of the unique code or update the new details by overwriting the existing details in Sharepoint Excel
Problem: I am unable to define a range using a variable (i) and specific cells row (cell.Row).
Current Code:
Sub TaskSearch()
'Dim wb As Workbook
Dim oSht As Worksheet
Dim lastRow As Long, i As Long
Dim strSearch As String
Dim aCell As Range
ThisWorkbook.Sheets("Interface").Range("D19:D33").ClearContents
'Set wb = Workbooks.Open("H:\Kevin.Boots\Database.xlsx")
Set oSht = Sheets("TaskMaster")
lastRow = oSht.Range("A" & Rows.Count).End(xlUp).Row
strSearch = Sheets("Interface").Range("F5")
Set aCell = oSht.Range("B2:B" & lastRow).Find(What:=strSearch, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False)
Sheets("Interface").Range("D19").Value = Sheets("TaskMaster").Range("C" & aCell.Row).Value
Sheets("Interface").Range("D20").Value = Sheets("TaskMaster").Range("D" & aCell.Row).Value
Sheets("Interface").Range("D21").Value = Sheets("TaskMaster").Range("E" & aCell.Row).Value
Sheets("Interface").Range("D22").Value = Sheets("TaskMaster").Range("F" & aCell.Row).Value
Sheets("Interface").Range("D23").Value = Sheets("TaskMaster").Range("G" & aCell.Row).Value
Sheets("Interface").Range("D24").Value = Sheets("TaskMaster").Range("H" & aCell.Row).Value
Sheets("Interface").Range("D25").Value = Sheets("TaskMaster").Range("I" & aCell.Row).Value
Sheets("Interface").Range("D26").Value = Sheets("TaskMaster").Range("J" & aCell.Row).Value
Sheets("Interface").Range("D27").Value = Sheets("TaskMaster").Range("K" & aCell.Row).Value
Sheets("Interface").Range("D28").Value = Sheets("TaskMaster").Range("L" & aCell.Row).Value
Sheets("Interface").Range("D29").Value = Sheets("TaskMaster").Range("M" & aCell.Row).Value
Sheets("Interface").Range("D30").Value = Sheets("TaskMaster").Range("N" & aCell.Row).Value
Sheets("Interface").Range("D31").Value = Sheets("TaskMaster").Range("O" & aCell.Row).Value
Sheets("Interface").Range("D32").Value = Sheets("TaskMaster").Range("P" & aCell.Row).Value
Sheets("Interface").Range("D33").Value = Sheets("TaskMaster").Range("Q" & aCell.Row).Value
Exit Sub
End Sub
Objective: I am attempting to make this code more robust. Part of reasoning is for me to be able to skip blanks. This is a nightmare when trying to adjust cells.
I have tried two different methods to accomplish this:
Method A:
wb.Sheets("Interface").Range("D19:D33").Copy
wb.Sheets("TaskMaster").Range("C" & aCell.Row & ":Q" & aCell.Row).PastSpecial Paste:=xlPasteValues, SkipBlanks:=True
Failure: Runtime Error 438: Object doesn't support this property or method.
Method B:
For j = 3 To 16
If Not IsEmpty(j, aCell.Row) Then
For i = 19 To 33
iWb.oSht.Range(j, aCell.Row).Value = _
iWb.iSht.Range(4, i).Value
Next i
End If
Next j
Exit Sub
Failure: ( I cant get this older version to compile again) I believe the error arose with issues defining the range.
To summarize I am trying to find the fastest method to transfer information from one worksheet to another worksheet using the .find. I am also trying to not copy blank cells while transferring.
I currently believe this method will be the best suited for my application.
Sub TSearch()
Dim dWb As Workbook, Wb As Workbook
Dim oSht As Worksheet, Sht As Worksheet
Dim lastRow As Long, i As Long, j As Long
Dim strSearch As String
Dim aCell As Variant
Dim cell As Variant
'On Error GoTo Err
ThisWorkbook.Sheets("Interface").Range("D19:D33").ClearContents
'Set dWb = Workbooks.Open("H:\Kevin.Boots\Database.xlsx")
Set Wb = ThisWorkbook
Set Sht = Sheets("TaskMaster") ' Reference Worksheet
Set oSht = Sheets("Interface") ' User Interface Worksheet
lastRow = Sht.Range("A" & Rows.Count).End(xlUp).Row 'Obtain Last row of reference Worksheet
strSearch = oSht.Range("F5") 'Obtain User Selected Search Criteria
Set aCell = Sht.Range("B2:B" & lastRow).Find(What:=strSearch, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False)
For j = 3 To 16 'Columns from Reference Worksheet to be transfered
If Not IsEmpty(Wb.Sht.Cells(aCell.Row, j)) Then ' Verify If cell has value before transfering
For i = 19 To 33 ' Rows of User Interface where values are to be transfered
Wb.Sht.Cells(aCell.Row, j).Value = _
Wb.oSht.Cells(i, 4).Value
Next i
End If
Next j
Exit Sub
'Err: 'MsgBox " Generic Task not found" & vbCrLf
End Sub
The IEmpty Function is still causing an error 438: Object doesn't support this property method. If I remove the IsEmpty then
'Wb.Sht.Cells(aCell.Row, j).Value = Wb.oSht.Cells(i, 4).Value' gives me the same error.
Your loop won't work due to IsEmpty which expects a single cell or variable to check, but you are giving it two numbers. The below should work, but some things aren't qualified, so you may still run into issues. Also, Range() expects either two ranges to be provided, or a range string. I think you were looking for Cells(), which accepts a row (as a number) as the first parameter and a column (as a number) as the second.
For j = 3 To 16
If Not IsEmpty(cells(aCell.Row, j)) Then
For i = 19 To 33
iWb.oSht.Cells(aCell.Row, j).Value = _
iWb.iSht.Cells(4, i).Value
Next i
End If
Next j
End Sub
It appears the errors were a result of trying to define something that was defined.
Sub TSearch()
Dim dWb As Workbook, Wb As Workbook
Dim oSht As Worksheet, Sht As Worksheet
Dim lastRow As Long, i As Long, j As Long
Dim strSearch As String
Dim aCell As Variant
Dim cell As Variant
'On Error GoTo Err
ThisWorkbook.Sheets("Interface").Range("D19:D33").ClearContents
'Set dWb = Workbooks.Open("H:\Kevin.Boots\Database.xlsx")
Set Wb = ThisWorkbook
Set Sht = Sheets("TaskMaster") ' Reference Worksheet
Set oSht = Sheets("Interface") ' User Interface Worksheet
lastRow = Sht.Range("A" & Rows.Count).End(xlUp).Row 'Obtain Last row of reference Worksheet
strSearch = oSht.Range("F5") 'Obtain User Selected Search Criteria
'Find Row in Reference Worksheet that Matches Search Criteria
Set aCell = Sht.Range("B2:B" & lastRow).Find(What:=strSearch, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False)
For j = 3 To 16
If Not IsEmpty(Cells(aCell.Row, j)) Then
i = j + 16
oSht.Cells(i, 4).Value = Cells(aCell.Row, j).Value
End If
Next j
Exit Sub
'Err:
'MsgBox " Generic Task not found" & vbCrLf
End Sub
Thank you to #Jordan and #Kyle for helping solve this issue.
When creating a blank Excel Workbook, the file size is kept small by not using all 1,000,000+ rows. However, once data has been added to all of the rows, the rows still exist even if the data is deleted out of them, resulting in a huge filesize. I know these rows still exist because of the size of the scrollbar and the fact that if I scroll to the bottom I end up at row 1,000,000+ which does not happen on a new Workbook. I guess this would be called the UsedRange in VBA?
I have inherited a Workbook from another developer which is in this situation and I'm looking for a way to reduce the size. I can copy the used data only into a new blank Workbook, but does anyone know of a way to reduce the actual UsedRange?
The following code does not work:
Sub test()
Dim r As Range
Set r = Range("10000:1000000")
r.Delete
End Sub
Nor does highlighting the rows manually, clicking in the header and clicking delete
Open VBA and type ActiveSheet.UsedRange in the immediate window.
I added it to my right-click menu since I use it so often.
Edit:
Here's the code I use to add it to the context menu:
Private Sub Auto_Open()
On Error Resume Next
'delete the control if it exists
Application.CommandBars.FindControl(Tag:="MY_TAG").Delete
On Error GoTo 0
With Application.CommandBars("Cell").Controls
'add reset range button
With .Add(Type:=msoControlButton)
.Caption = "Reset used range"
.OnAction = ThisWorkbook.Name & "!ResetRange"
.Tag = "MY_TAG"
.BeginGroup = True
End With
End With
End Sub
Private Sub ResetRange()
ActiveSheet.UsedRange
End Sub
To clear all unused rows and columns in all sheets :
Sub ReduceFileSize()
Dim wB As Workbook
Dim wS As Worksheet
Set wB = ThisWorkbook
For Each wS In wB.Sheets
DeleteUnUsed wS
Next wS
wB.Save
End Sub
Sub DeleteUnUsed(wS As Worksheet)
Dim r As Range
Dim LastRow As Double
LastRow = LastRow_1(wS)
Dim LastCol As Double
LastCol = LastCol_1(wS)
With wS
.Range(LastRow + 1 & ":" & .Rows.Count).EntireRow.Delete
.Range(collet(LastCol) & ":" & collet(.Columns.Count)).EntireColumn.Delete
End With
End Sub
Public Function LastRow_1(wS As Worksheet) As Double
With wS
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
LastRow_1 = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
LastRow_1 = 1
End If
End With
End Function
Public Function LastCol_1(wS As Worksheet) As Double
With wS
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
LastCol_1 = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
Else
LastCol_1 = 1
End If
End With
End Function
Public Function ColLet(x As Integer) As String
With ActiveSheet.Columns(x)
ColLet = Left(.Address(False, False), InStr(.Address(False, False), ":") - 1)
End With
End Function
I have maintained two Excel reports EPC1.xlsx and Control Power Transformers.xlsm respectively.
I want to trigger an button click from Control Power Transformers.xlsm report where it will search for "CTPT" term in "A" column from EPC1.xlsx, once it finds the term it need to copy Column B and Column c till the row ends (in EPC1.xlsx) and paste it in Control Power Transformers.xlsm workbook
I am successful in retrieving the cell address of "CTPT" term but how to select the data from adjacent column B and C?
And this is what I have tried
Private Sub CommandButton23_Click()
Dim rngX As Range
Dim num As String
Windows("EPC 1.xlsx").Activate
Set rngX = Worksheets("Sheet1").Range("A1:A10000").Find("CTPT", Lookat:=xlPart)
num = rngX.Address ' Here we will the get the cell address of CTPT ($A$14)
Range(rngX, Range("C" & rngX.Row).End(xlDown)).Copy
Windows("Control Power Transformers.xlsm").Activate
Sheets("Sheet2").Select
ActiveSheet.Range("E2").PasteSpecial (xlPasteValues)
End Sub
Paste the below in sample workbook. The below code will help to select both files using file dialog. It will search for word "CTPT". if so it will copy the column values from CTPT sheet to control file.
Sub DetailsFilePath()
Dim File1 As String
Dim File2 As String
Dim findtext As String
Dim copyvalues As Long
Dim c As Variant
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
MsgBox "Open the CTPT file"
Application.FileDialog(msoFileDialogFilePicker).Show
'On Error Resume Next
' open the file
File1 = Application.FileDialog(msoFileDialogFilePicker).SelectedItems(1)
MsgBox "Open the Control Power Transformers file"
Application.FileDialog(msoFileDialogFilePicker).Show
File2 = Application.FileDialog(msoFileDialogFilePicker).SelectedItems(1)
Set wb1 = Workbooks.Open(Filename:=File1)
Set ws1 = wb1.Worksheets("sheet1")
Set wb2 = Workbooks.Open(Filename:=File2)
Set ws2 = wb2.Worksheets("sheet1")
findtext = "CTPT"
With ws1.Columns(1)
Set c = .Find(findtext, LookIn:=xlValues)
If Not c Is Nothing Then
copyvalues = c.Column
ws2.Columns(2).Value = ws1.Columns(2).Value
ws2.Columns(3).Value = ws1.Columns(3).Value
End If
End With
wb1.Close savechanges:=True
wb2.Close savechanges:=True
End Sub
You need to use FindNext to find other results, and the Offset will help you select what you want from the address of your results :
Sub test_Karthik()
Dim WbEPC As Workbook, _
WbCPT As Workbook, _
WsEPC As Worksheet, _
WsCPT As Worksheet, _
FirstAddress As String, _
WriteRow As Long, _
cF As Range, _
num As String
Set WbEPC = Workbooks("EPC 1.xlsx")
Set WbCPT = Workbooks("Control Power Transformers.xlsm")
Set WsEPC = WbEPC.Sheets("Sheet1")
Set WsCPT = WbCPT.Sheets("Sheet2")
With WsEPC
.Activate
With .Range("A1:A10000")
'First, define properly the Find method
Set cF = .Find(What:="CTPT", _
After:=ActiveCell, _
LookIn:=xlValues, _
Lookat:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
'If there is a result, keep looking with FindNext method
If Not cF Is Nothing Then
FirstAddress = cF.Address
Do
num = cF.Address ' Here we will the get the cell address of CTPT ($A$14)
WsEPC.Range(cF.Offset(0, 1), cF.Offset(0, 2).End(xlDown)).Copy
WriteRow = WsCPT.Range("E" & WsCPT.Rows.count).End(xlUp).Row + 1
WsCPT.Range("E" & WriteRow).PasteSpecial (xlPasteValues)
Set cF = .FindNext(cF)
'Look until you find again the first result
Loop While Not cF Is Nothing And cF.Address <> FirstAddress
End If
End With
End With
End Sub
I was trying to compare two different Workbooks, one named "after" (which is up to date) and another called "before" and I wanted to highlight the differences so it would be easy to pin down what changed between the two.
Okay so after I tested it a bit I got stuck in an error
"Object doesn't support this property or method".
Here's the full code with added comments so you can follow my thought process:
Sub OpenCsv()
Dim zcf, FolderPath, after, before, shtAfter, shtBefore As String
Dim MotherWB As Workbook, MotherWS As Worksheet
Dim wb As Workbook, ws, worksheetz As Worksheet
Dim oneRange, aCell As Range
Dim rng As Range
Dim Answer As Integer
Dim mycell As Range
Dim mydiffs As Integer
'Sorts Things for MotherWB
Set oneRange = Range("A4:Z9000")
Set aCell = Range("F4")
oneRange.Sort Key1:=aCell, Order1:=xlAscending, Header:=xlYes
'Opens and sets both Workbooks with their respective sheets
FolderPath = Application.ActiveWorkbook.Path
after = FolderPath + "\" + "after.csv"
before = FolderPath + "\" + "before.xlsm"
Workbooks.Open (after)
Set wb = Workbooks("after.csv")
Set ws = wb.Worksheets("after")
Set MotherWB = Workbooks("before.xlsm")
Set MotherWS = MotherWB.Worksheets("before")
'Makes ws looks like MotherWS so we compare them
With ws
Columns("A:Z").AutoFit
Selection.TextToColumns _
Destination:=Range("A1:A9000"), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, _
Semicolon:=False, _
Comma:=True, _
Space:=False, _
Other:=False
Set oneRange = Range("A4:Z9000")
Set aCell = Range("F4")
oneRange.Sort Key1:=aCell, Order1:=xlAscending, Header:=xlYes
End With
'Questions if you want to compare both
Answer = MsgBox("Uma vez aberto o relatório deseja comparar os dois?", vbYesNo + vbQuestion, "Comparar")
If Answer = 6 Then
'For each cell in after that is not the same in before, color it yellow
For Each mycell In wb.ws(after).UsedRange
If Not mycell.Value = MotherWB.MotherWS(before).Cells(mycell.row, mycell.Column).Value Then
mycell.Interior.Color = vbYellow
mydiffs = mydiffs + 1
End If
Next
'Display a message box to demonstrate the differences
MsgBox mydiffs & " differences found", vbInformation
ActiveWorkbook.Sheets(after).Select
End If
End Sub
It gets the error after I return 6 from the answer, saying what I stated above. What am I doing wrong?
Answer is set as boolean and the msgbox will return a integer. Declare an integer and then use an if statement to put true/false in your answer variable. Something similar to below
Dim temp as integer
temp = MsgBox("Uma vez aberto o relatório deseja comparar os dois?", vbYesNo + vbQuestion, "Comparar")
if temp = 6 then
Answer = true
else
Answer = false
endif