vba excel counta - vba

Cells(4, x) = Application.WorksheetFunction.COUNTA(Workbooks(""DB_Report.xls"").Sheets(x).Range(A:A))
I am trying to get the above function to work.
I am calling the script from the workbook DB_report.xls
This creates a new workbook ("month") and starts filling in the values.
What I am trying to get to is where
cell 4,1 in months has the counta of sheet 1 from DB_report
cell 4,2 in months has the counta of sheet 2 from DB_report
Can anyone reword the line above so when "months is the active work sheet I can call counta from DB_Report
The line before this is
NameSH = Workbooks("DB_Report.xls").Sheets(x).Name and this works fine and returns the name of work sheet x
Thanks
Aaron
Ok for a bit further explicanation
the steps I want to do go some thing like this
select workbook months.xls
select sheet(1)
cell (x,y) = counta( of range A:A , in worksheet("DB_Report") of worksheet (DB_report.xls)
Now I know
Cells(4, x) = Application.WorksheetFunction.COUNTA(sheet(3).range(a:A)
will work with in the active work sheet. So if the active sheet is sheet 1 then that would count up he numbe of cells in sheet 3 of the same workbook. I wanted to know if as well as refrenced sheet and cells in the function I can also refrence a workbook by name.
of course i could swqap to book "DB_Report" save the value to a varible and then swap back to book "Month" and copy it to the cell.
or could I do workbook("month").sheet(y).cells(a,b) = Application.WorksheetFunction.COUNTA(sheet(3).range(a:A)
while in workbook "month"
so really what i need is how do you refrence workbook,sheet and cells all with in a function?

I don't think this is exactly what you were trying to do, but it comes close and is a bit more generalized. It counts up the worksheets in what would be DB_Report.xls and uses that to specify that number of cells in months.xls
If you are running the macro from the DB_Report.xls you don't need to specify anything about that workbook or sheets.
Sub counts()
Dim sheetcounts As Integer
Dim countas() As Integer
Dim index As Integer
Dim wksht As Worksheet
Dim newbook As Workbook
sheetcounts = ActiveWorkbook.Sheets.Count
ReDim countas(sheetcounts)
For Each wksht In ActiveWorkbook.Sheets
countas(index) = Application.WorksheetFunction.CountA(wksht.Range("A:A"))
index = index + 1
Next
Set newbook = Workbooks.Add
newbook.Activate
newbook.ActiveSheet.Range(Cells(4, 1), Cells(4, sheetcounts)) = countas
newbook.SaveAs ("months.xls")
End Sub
It will require any error checking or verification that you need to put into it.

Hi Cheers for the comments, but i finaly worked out what the problem was.
it was simple really I was just missing some formatting
the line below works correctly
cell(x,y) = Application.WorksheetFunction.CountA(Workbooks("DB_Report.xls").Sheets(x).Range("A:A"))

Related

Entering formula into column range but select method of range class failed

please let me know if I need to be more specific. I am entering a weeknum() formula on my workbook isum on sheet stock. I want this formula to be evaluated for everything in the "X" column starting at row 2 since the top row will be a label. I used a macro recorder, but I am getting an error 1004 because my "select method of range class failed". There is probably a better way of writing this code. After, I want to make any weeknum value that is less than 27 in this column "Past". isum is my workbook and stock is my sheet in this workbook. I just made the range up to a certain amount rather than the used amount that I want to have. Here is my code so far: isum.Sheets("stock").Range("X2").Select
ActiveCell.FormulaR1C1 = "=WEEKNUM(RC[-9])"
Selection.AutoFill Destination:=Range("X2:X900").Select
Range("X2:X900").Select
Your code could be simplified to:
'Declare variables
dim wb as Workbook
dim ws as Worksheet
dim rng as Range
'Set variables
set wb = Workboooks("isum.xlsx") 'check the file extension & name is correct
set ws = wb.Worksheets("stock")
set rng = ws.range("X2:X900")
'Fill range with formula
rng.FormulaR1C1 = "=WEEKNUM(RC[-9])"
(Link - Here's a great reference on how/why to avoid using SELECT in VBA)
Edit: another way to set the workbook:
set wb = ThisWorkbook

Copy multiple columns of values from one workbook to corresponding sheets on another workbook

I have been trying to get this to work, but I am not sure how to get it to work, any help would be appreciated.
I have 2 workbooks, workbook 1 has multiple sheets, each one labelled with a different name. Workbook 2 has one summary sheet with a column of values for each individual.
What I am trying to achieve is:
on workbook 1 check the sheet name
switch to workbook 2 and find the column with the same name. All the names are on row 6, from column I to DD. Also, each name is in 2 cells merged together, I don't know if this affects it.
Once the name on row 6 is found, I want it to go down 6 cells, and copy the value.
switch back to workbook 1 and paste it into cell B37.
Repeat this process but this time go down 7 cells, copy the value and paste it into cell B102 OF Workbook 1. I have about 30 cells to copy and past like that.
once complete repeat everything again for the next sheet on workbook 1.
Another Important issue is that, not all sheet names on workbook 1 exists on workbook 2, I have a feeling the below code will throw an error as soon as it doesn't find a match. So I would like to be able to skip the sheets on workbook 1 that it doesn't find a matching name for on workbook 2 summary sheet.
I have put the code I have below, but I keep getting the error "Method or data member not found"
Sub Measures()
Dim wb1 As Workbook
Dim Sht As Worksheet
Dim Rng, Rng2 As Range
Dim wb2 As Workbook
Dim cell As Range
Dim ws As Worksheet
Set wb1 = ThisWorkbook
Set wb2 = Workbooks("November Stream 1.xlsm")
Set Sht = wb1.Worksheets("Summary")
Set Rng = Sht.Range("A6:A" & Sht.Cells(Sht.Column.Count, "A").End(xlUp).Column)
For Each cell In Rng
Set ws = wb2.Sheets(cell.Text)
ws.Range("B37").Value = cell.Offset(6, 0).Value
ws.Range("B102").Value = cell.Offset(7, 0).Value
Next cell
End Sub
Thank you for any help!

I need a code to copy specific data from daily-updated excel workbook to my main excel workbook

I have a main excel workbook (1) with many tables but contains same columns as;
productnumber________name_______surname_______price
xxx
Have another workbook (2) which contains columns as;
productnumber________city_______company_______price
What I have tried?
I have 10000 products in workbook 1, 500 products in workbook 2, so if i use VLOOKUP function on (xxx) cell by looking up the "productnumber" in other sheet, I get "Na" error in some of the cells due to a lack of enough productnumber in daily updated workbook (2)
Second workbook (2) is being updated everyday and I just want to copy and paste the daily prices to my main workbook (1) and
what is the shortest way to do it? How can I use VBA to do that? Which code would help me?
Thank you
Short of having enough information about the names of your objects, try adapting this working code to your names:
Sub refreshPrices()
Dim cel As Range, wbNewPrices As Workbook, v
Set wbNewPrices = Workbooks.Open("C:\SO\newPrices.xlsm")
For Each cel In ThisWorkbook.Sheets(1).Range("Table1[productnumber]")
v = Application.VLookup(cel.value, wbNewPrices.Sheets(1).Range("Table1"), 4, 0)
If Not IsError(v) Then cel.Offset(, 3).value = v
Next
wbNewPrices.Close False
End Sub

Excel VBA get the name of the next sheet after current

Is there a way in Excel VBA to get the name of the next sheet following the ActiveSheet ?
For example let's presume that my spreadsheet has 4 sheets and I would like to create a macro which inserts in the current worksheet (Sheet1 in this case) the name of the next spreadsheet (Sheet2). Like this:
Activesheet.Index will give you the position of the active sheet, so:
ActiveSheet.Range("A1")= ActiveSheet.Parent.Sheets(Activesheet.Index + 1).Name
You should add a check that the activesheet isn't the last one...
Just a little sample here.
Dim sh As Worksheet, shprv As Worksheet, shnext As Worksheet
//make sure that use "as Worksheet" after every variable you declare
Set sh = ActiveSheet //always use "Set" before since these variables are "Object type", only by this their values can be set correctly
Set shprv = sh.Previous
Set shnext = sh.Next
In this case, the value of sh will be Sheet1 as the picture you gave,
and the value of shnext will be Sheet2.
But shprv will have no value since there is no sheet before Sheet1.

Copy cells between workbooks

Could someone please help me with some VBA code.
I am trying to copy 2 ranges of cells between workbooks (both workbooks should be created beforehand as i don't want the code to create a new workbook on the fly).
Firstly I need to copy these ranges-
From 'Sheet 3' of booka.xls, Range: Cell H5 to the last row in column H with data
copy this to 'Sheet 1' of bookb.xls, starting in Cell B2 for as many cells down in the B column
Secondly I need to copy these ranges-
From 'Sheet 3' of booka.xls, Range: Cell K5 to the last row in column K with data
copy this to 'Sheet 1' of bookb.xls, starting in Cell D2 for as many cells down in the D column
Here is what I have so far:
Sub CopyDataBetweenBooks()
Dim iRow As Long
Dim wksFr As Worksheet
Dim wksTo As Worksheet
wksFr = "C:\booka.xls"
wksTo = "C:\bookb.xls"
Set wksFrom = Workbooks(wksFr).Worksheets("Sheet 3")
Set wksTo = Workbooks(wksTo).Worksheets("Sheet 1")
With wksFrom
For iRow = 1 To 100
.Range(.Cells(iRow, 8), .Cells(iRow, 9)).Copy wksTo.Cells(iRow, 8)
Next iRow
End With
End Sub
Assuming you have the reference to wksFrom and wksTo, here is what the code should be
wksFrom.Range(wksFrom.Range("H5"), wksFrom.Range("H5").End(xlDown)).Copy wksTo.Range("B2")
wksFrom.Range(wksFrom.Range("K5"), wksFrom.Range("K5").End(xlDown)).Copy wksTo.Range("D2")
Here's an example of how to do one of the columns:
Option Explicit
Sub CopyCells()
Dim wkbkorigin As Workbook
Dim wkbkdestination As Workbook
Dim originsheet As Worksheet
Dim destsheet As Worksheet
Dim lastrow As Integer
Set wkbkorigin = Workbooks.Open("booka.xlsm")
Set wkbkdestination = Workbooks.Open("bookb.xlsm")
Set originsheet = wkbkorigin.Worksheets("Sheet3")
Set destsheet = wkbkdestination.Worksheets("Sheet1")
lastrow = originsheet.Range("H5").End(xlDown).Row
originsheet.Range("H5:H" & lastrow).Copy 'I corrected the ranges, as I had the src
destsheet.Range("B2:B" & (2 + lastrow)).PasteSpecial 'and destination ranges reversed
End Sub
As you have stated in the comments, this code above will not work for ranges with spaces, so substitute in the code below for the lastrow line:
lastrow = originsheet.range("H65536").End(xlUp).Row
Now ideally, you could make this into a subroutine that took in an origin workbook name, worksheet name/number, and range, as well as a destination workbook name, worksheet name/number, and range. Then you wouldn't have to repeat some of the code.
You can use special cells like Jonsca has suggested. However, I usually just loop through the cells. I find it gives me more control over what exactly I am copying. There is a very small effect on performance. However, I feel that in the office place, making sure the data is accurate and complete is the priority. I wrote a response to a question similar to this one that can be found here:
StackOverflow - Copying Cells in VBA for Beginners
There is also a small demonstration by iDevelop on how to use special cells for the same purpose. I think that it will help you. Good luck!
Update
In response to...
good start but it doesn't copy anything after the first blank cell – trunks Jun 9 '11 at 5:08
I just wanted to add that the tutorial in the link above will address the issue brought up in your comment. Instead of using the .End(xlDown) method, loop through the cells until you reach the last row, which you retrieve using .UsedRange.Rows.Count.