Is there a way to get the max number of user filled cells in an excel sheet without passing a range or iterating through max cells per excel spreadsheet?
right now I have
Imports Excel = Microsoft.Office.Interop.Excel
Private Sub DoStuff()
Dim oApp As New Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
oWB = oApp.Workbooks.Open(txtExcel.Text)
oSheet = CType(oWB.ActiveSheet, Excel.Worksheet)
For Each iCount As Integer in oSheet.Cells.Rows.Count
'get cell text for every row in a specific column'
dim sCellText as string = oSheet.Cells(iCount, 1).toString
'do stuff'
Next
End Sub
Right now the oSheet.Cells.Rows.Count = 1048576 and looking through the sheet there is less than 1000 rows of data.
Try oSheet.UsedRange
Here you can check for the column in the Range instance returned by above, to figure out cells filled in the specific column.
OR
WorksheetFunction.CountA(range("A1:A20000"))
Related
I need to create a macro in my CountResults.xlsm (Master Workbook) that solves the following problem. I have a column of data in another worksheet with either YES or NO. I need to come up with a macro that counts the amount of "YES" in the column. The column is located in Sheet2 of the workbook Test01.xlsx. Then take that count and put it in one cell in my CountResults.xlsm file. Like so:
I have a code that displays a count for a column in the same sheet. But this code does not count when there are 'breaks' in the column (empty spaces) like I have in my picture. This is that code:
Private Sub CommandButton1_Click()
MsgBox Range("A1").End(xlDown).Row
Range("A1").End(xlDown).Offset(1, 0).Select
End Sub
I have another code that helps with accessing another workbook and defining values for each workbook and worksheet:
Dim wbSource As Workbook
Dim wbTarget As Workbook
Dim shSource As Worksheet
Dim shTarget As Worksheet
Set wbSource = Workbooks.Open(Filename:="C:\Users\khanr1\Desktop\Test_Excel\Test03.xlsm", ReadOnly:=True)
Set wbTarget = ThisWorkbook
Set shSource = wbSource.Worksheets("Sheet2")
Set shTarget = wbTarget.Worksheets("Sheet1")
Use COUNTIF. It will give you the total even if the range is in another workbook. i.e. =COUNTIF([Book2.xlsx]Sheet2!$D$2:$D$9, "Yes"). Problem with having COUNTIF within your sheet as a formula is that you will need to open the other workbook if you want the count to be update. Below VBA code will perform an update for you. Assign the sub to a button in your CountResults.xlsm workbook
EDIT: Added row count as per OP's requirement
Sub UpdateResults()
Dim oWBWithColumn As Workbook: Set oWBWithColumn = Application.Workbooks.Open("<your Test01.xlsx address here>")
Dim oWS As Worksheet: Set oWS = oWBWithColumn.Worksheets("Sheet2")
Dim intLastRow as Integer: intLastRow = oWS.Cells(Rows.Count, "B").End(xlUp).Row
ThisWorkbook.Worksheets("<name of the sheet in your CountResults.xlsm workbook>").Range("<cell address>").Value = Application.WorksheetFunction.CountIf(oWS.Range("B2:B" & intLastRow), "yes")
oWBWithColumn.Close False
Set oWS = Nothing
Set oWBWithColumn = Nothing
End Sub
I'm trying to get my application to read a excel file that works with named ranges.
My excel has alot of named ranges, and I would like to access the values in these named ranges to fill my combobox on my windows-user-form.
But i have no idea how get these values.
For now i have this to access my excel spreadsheet + range
Dim excel As Application = New Application
Dim w As Workbook = excel.Workbooks.Open("C:\temp\test.xlsm")
Dim rng As Range
rng = w.Worksheets("Sheet1").Range("range")
w.Close()
On this "Sheet1" there is a Named Range called "Range" and it contains 6 cells. I want to add these cell values to my combobox1 using the Named Range.
But i can't figure out a way to do this.. Can someone help me? :)
You can try using Goto and Selection. Here is a sample that returns an array of values, as per your requirement:
Dim xl As Excel.Application
Dim wb As Excel.Workbook
xl = New Excel.Application
wb = xl.Workbooks.Open("C:\Temp\range.xlsm")
xl.Goto("NewNamedRange")
Dim rng As Excel.Range = xl.Selection
Debug.Print(rng.Value.ToString)
I am wondering if someone can help me out. I created a Userform with 3 comboboxes. Combobox 1 and 2 list all open workbooks. Combobox 3 lists the worksheets from Combobox 2. I now want to run a Vlookup. The lookup values are the values (in this case product codes) in each cell beginning at D9 to the last cell with a value in Column D of the first Worksheet of Combobox2's. The lookup range will be ("A5:S###"[number of rows varies depending on the file]").
The Vlookup formula should be in the Column I of the first Worksheet of Combobox2's value starting at "I9" looping through each cell in I9 until all the Codes in D9 are looked up.
I keep getting error the major one being “Runtime-error '9'”: Subscript out of range. Here is my code.
Option Explicit
Private Sub CancelButton_Click()
Stopped = True
Unload Me
End Sub
Private Sub ComboBox1_Change()
Dim ScheduleA As Workbook
Dim Termset As Worksheet
Set ScheduleA = Workbooks(Me.ComboBox1.Value)
With Me.ComboBox3
For Each Termset In ScheduleA.Worksheets
.AddItem Termset.Name
Next Termset
End With
End Sub
Private Sub FillACDButton_Click()
Dim ACDRebateInfo As Worksheet
Dim lastRow As Long
Dim NewRebate As Single
Dim NewRebateType As String
Dim LookUp_Range As Range
Dim ActionCode As String
Dim ACD_NewRebate As Range
Dim ACD_NewRebateType As Range
Dim ACD_ActionCode As Range
Dim SCC As Range
Dim Cell As Range
Set ACDRebateInfo = Workbooks(Me.ComboBox2.Value).Worksheets(1)
Set ACD_NewRebate = ACDRebateInfo.Range("I9:I500")
Set ACD_NewRebateType = ACDRebateInfo.Range("J9:J500")
Set ACD_ActionCode = ACDRebateInfo.Range("B9:B500")
Set LookUp_Range = Worksheets(Me.ComboBox3.Value).Range("A5:S400")
Set SCC = ACDRebateInfo.Range("D9:D230")
With ACDRebateInfo
For Each Cell In ACD_ActionCode
ActionCode = Application.WorksheetFunction.VLookup(SCC, LookUp_Range, 17, False)
Next Cell
End With
Unload Me
End Sub
Private Sub UserForm_Initialize()
Dim wkb As Workbook
For Each wkb In Application.Workbooks
Me.ComboBox1.AddItem wkb.Name
Me.ComboBox2.AddItem wkb.Name
Next wkb
End Sub
Not sure this is your issue but this piece of code does not make sense:
For Each Cell In ACD_ActionCode
ActionCode = Application.WorksheetFunction.VLookup(SCC, LookUp_Range, 17, False)
Next Cell
You are looping through the Action Codes but not using the Cell variable
I would like to append data using word as front-end userform and export the data to specific csv file when submit button is clicked.
While firstly I need to know which row I should append the data. Here is the following code and I got error message for nrow = rng1.UsedRange.Rows.Count lines
'Reference - Microsoft Excel 12.0 Object library 12
'Word
Global objDoc As word.Document
Global docName As String
Global docPath As String
Global docFullName As String
'Workbook
Global objWb As Excel.Workbook
Global objWs As Excel.Worksheet
Global rng1 As Range
Global nrow As Integer
'Global objWord As word.Application
Public Sub Initial_Global()
'Word
Set objDoc = ActiveDocument
docName = objDoc.Name
docPath = objDoc.Path
docFullName = objDoc.FullName
'Excel
Set objWb = Workbooks.Open(FileName:="C:\Users\1502911\Desktop\Database1.csv")
Set objWs = objWb.Worksheets("Database1")
Set rng1 = objWs.Range("A1")
nrow = rng1.UsedRange.Rows.Count
MsgBox nrow
End Sub
nrow = rng1.UsedRange.Rows.Count
this means:
Worksheets("Database1").Range("A1").UsedRange.Rows.Count 'wrong
of course it will return error, because you asking the used range of the range "A1"
must be like this:
Worksheets("Database1").UsedRange.Rows.Count
so in your code, if you required to count only rows in used range of the column "A" must be the next:
nrow = objWs.UsedRange.Columns(1).Rows.Count
but be carefull with this method of the getting the last row in sheet, the problem will apear when the used range will not start from the first row, e.g. if the first row will be empty then you will recieve count less then expected. in this case use this method:
nrow = objWs.Cells(Rows.Count, 1).End(xlUp).Row
also can be the problem, when e.g. count of the used cells in column "A" less by the some reason than count of the used cells in Columns "B" or "D " etc. but you need to get the last non-used row, in this case you need this method:
Dim ocell As Range
For Each ocell In objWs.UsedRange
nrow = ocell.Row
Next
MsgBox nrow
Hello so I am trying to find an exact string in a excel workbook and count how many times it in that sheet and was stuck on how I should do this can anyone help?
'Open a blank Excel File.
oXL = CType(CreateObject("Excel.Application"), Excel.Application)
'Sets oxl visible off so you can't see it open.
oXL.Visible = True
' Get a new workbook.
oWB = oXL.Workbooks.Open(dicJobList)
'Set oSheet to active sheet.
oSheet = CType(oWB.ActiveSheet, Excel.Worksheet)
FillGrid()
oSheet.Cells.Find("DC")
'This is where I would need to count how many times
'It was found.