I need to remove the whole row IF column A value is empty. I have been doing it in excel and this method works best for me
.Columns("a:a").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
I need to do the same thing using vbs however, I have issues converting it to vbs
How can I convert the above line into vbs?
I looked up the xlCellTypeBlanks = 4 using F2 . But how to use the SpecialCells method ?
Something like this
Const xlCellTypeBlanks = 4
Dim xlApp
Dim xlwb
Set xlApp = CreateObject("Excel.Application")
Set xlwb = xlApp.workbooks.Open("C:\temp\test.xlsm")
On Error Resume Next
xlwb.Sheets(1).Columns("a:a").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
You will need to have an object for the Excel App, Workbook, Worksheet etc.. So something like
Dim xlApp as Object
Set xlApp = CreateObject("Excel.Application")
Dim wb as object
Set wb = xlApp.Open("YourWorbookName.xlsx")
wb.Worksheets("NameOfWorksheet").Columns("a:a").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Related
I have a simple Textbox in Excel worksheet (.xlsx) where I can read the BackColor property in Excel VBA sub with:
Debug.Print TextBox1.BackColor
I'm trying to reference that same textbox from MS-Access using the Excel 16 Object Model, but it can't see the textbox under any of the Excel Worksheet objects I'm looking at
It errors out on the line marked with asterisks below with error message
Method or Data Member Not Found
Public Sub SetHexColor()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = GetObject(, "Excel.Application")
Set xlBook = Workbooks.Open("C:\Users\.........\Documents\TextBox.xlsx")
Set xlSheet = xlBook.Worksheets(1)
**Debug.Print xlSheet.TextBox1.BackColor**
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
Is there another way to reference and preferably set properties of a Textbox control in Excel?
I'd prefer not to have call an Excel function to set the property if possible - or maybe that's the issue - it has to be an xlsm file?
Excel.Worksheet is a generic worksheet object - it only provides access to "out of the box" methods of the Worksheet object: if you've "subclassed" your worksheet by adding members such as TextBox1 then you can't access those added members via the generic Worksheet type.
You can either do this
Dim xlSheet As Object
or leave the declaration as-is, and use something like
Debug.Print xlSheet.OLEObjects("TextBox1").Object.BackColor
Note this is not specific to automating Excel from access VBA - the same would be true if working entirely within Excel.
I am opening an excel workbook from excel and delete a sheet inside. I want to do this without the message from Excel:
"Excel will permanently delete this sheet, do you want to continue"
However I cannot make the "DisplayAlerts = False" work correctly.
Public Sub xportQuery()
Dim appExcel As Excel.Application
Dim myWorkbook As Excel.Workbook
Dim PathDaily, FileName As String
PathDaily = Forms!Menu!Text69
FileName = Forms!Menu!Text84
Set appExcel = CreateObject("Excel.Application")
Set myWorkbook = appExcel.Workbooks.Open(PathDaily & FileName)
appExcel.Visible = True
'Set appExcel = Nothing
Set myWorkbook = Nothing
appExcel.DisplayAlerts = False
Workbooks(FileName).Sheets("Sheety").Delete
appExcel.DisplayAlerts = True
End Sub
Writing it this way I get a "Subscript out of range" on the Sheets("Sheety").delete
If I take of the two DisplayAlerts lines, the code works but with the alert.
How do I write my code correctly to work without any alert message?
Note: DoCmd.SetWarnings didn t work either as the message is displayed in Excel
Thanks To Sam's Comment:
it works either by changing
Workbooks(FileName).Sheets("Sheety").Delete
into
myWorkbook.Sheets("Sheety").Delete
or
AppExcel.Workbooks(FileName).Sheets("Sheety").Delete
However the rest of the macro can still use Workbooks(Filename) without the "AppExcel." Reference
I am working in Access and need to pull data from an Excel file. The file is very large with several sheets that I don't need, so I want to take the values from one sheet and put them in a new workbook in order to simplify/speed up later calculations. Currently, it is including formatting along with the values and some of my later macros are not working even though they worked when I manually pasted the values into the new sheet. I tried running this directly in Excel (removing the references to the Excel.Application variable) and it worked fine. Is there something different I should be doing since this is being run in Access?
Private Sub CopyImportFile()
Dim xl As Excel.Application
Dim SourceBook As Excel.Workbook
Dim SourceSheet As Excel.Worksheet
Dim ImportBook As Excel.Workbook
Dim ImportSheet As Excel.Worksheet
Dim SourceSheetPath As String
SourceSheetPath = "File Path"
Set xl = CreateObject("Excel.Application")
Set SourceBook = GetObject(SourceSheetPath)
Set SourceSheet = SourceBook.Worksheets("Data Tape")
Set ImportBook = xl.Workbooks.Add()
SourceSheet.UsedRange.Copy
ImportBook.Sheets(1).Cells(1,1).PasteSpecial Paste:=xlPasteValues
xl.ActiveWorkbook.SaveAs FileName:="New File Path"
SourceBook.Close False
ImportBook.Close
Set xl = Nothing
Set ImportBook = Nothing
Set SourceBook = Nothing
Set ImportSheet = Nothing
I am relatively new to VBA, and am at a loss as to why I cannot paste into Excel from Word. The following macro ends up pasting the value into Word, even though I think I'm activating the Excel document.
The macro is meant to be run from Word; values from various FormFields then need to be pasted into cells in an existing Excel file.
I searched for a similar issue, though what was returned seemed to be variations of what I am experiencing, and I could not modify those answers to this.
Any help would be appreciated.
Sub Transfer()
Dim WD As Object
Dim ED As Excel.Application
Dim EDS As Excel.Workbook
Set WD = ActiveDocument
Set ED = CreateObject("excel.application")
ED.Visible = True
Workbooks.Open FileName:= _
"C:\Users\Documents\AppealData.xlsx"
ActiveWorkbook.Activate
Set EDS = ActiveWorkbook
WD.FormFields("AppNum").Copy
EDS.Activate
EDS.Sheets("Sheet1").Range("A1").Select
Selection.Paste
End Sub
Your Selection is referring to the current application. To refer to the Excel application you need to use ED.Selection. But it is a bad idea to rely on Activate and Select anyway.
I suggest you change your code to:
Sub Transfer()
Dim WD As Document
Dim ED As Excel.Application
Dim EDS As Excel.Workbook
Set WD = ActiveDocument
Set ED = CreateObject("excel.application")
ED.Visible = True
'Avoid "Activate"
Set EDS = ED.Workbooks.Open(FileName:= _
"C:\Users\Documents\AppealData.xlsx")
WD.FormFields("AppNum").Copy
'Avoid "Activate" and "Select" and "Selection"
'"Paste" is a worksheet Method, use "PasteSpecial" for a Range
'Use "xlPasteValues" to avoid formatting issues
EDS.Sheets("Sheet1").Range("A1").PasteSpecial Excel.xlPasteValues
End Sub
This here should work for you.
Sub Transfer()
Dim oExcel As Excel.Application
Dim oWB As Workbook
Set oExcel = New Excel.Application
Set oWB = oExcel.Workbooks.Open("C:\Users\Documents\AppealData.xlsx")
oExcel.Visible = True
Workbooks("Book1").Worksheets("Sheet1").Cells(1, 1).Value = _
CStr(Documents("Document1").FormFields("AppNum").Result)
End Sub
I'm trying to accomplish being able to find other Excel instances and control them using VBA.
Studying other threads this is what I've come up with so far, but for some reason it doesn't work.
What I'm trying to do is the following:
Dim xl as Excel.Application
Dim objList As Object
Dim objProcess As Object
Set objList = GetObject("winmgmts:")._
ExecQuery("select * from win32_process where name='Excel.exe'")
If objList.Count > 1 Then
For Each objProcess In objList '
If objProcess <> Application Then '
Set xl = objProcess ' this is what doesn't work
Exit For '
End If '
Next '
Else
Set xl = New Excel.Application
End If
'Do stuff with xl
Can anyone please tell me where I'm going wrong or if this is this even possible?
Have a look at this code I quickly put together, this worked for me when I had 5 instances of excel open, each with it's own workbook open. The code pops open a message box for all of the open workbooks in all of the instances of excel and displays the workbook name. You should be able to amend this to assign that workbook or instance of Excel to a variable instead.
Sub Testing()
Dim xlApp As Excel.Application
Set xlApp = GetObject(, "Excel.Application")
Dim xlWB As Excel.Workbook
For Each xlWB In xlApp.Workbooks
MsgBox xlWB.Name
Next xlWB
Set xlApp = Nothing
Set xlWB = Nothing
End Sub