Determine the number of rows in a range [closed] - vba

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I know the range name of the start of a list - 1 column wide and x rows deep.
How do I calculate x?
There is more data in the column than just this list. However, this list is contiguous - there is nothing in any of the cells above or below or either side beside it.

Sheet1.Range("myrange").Rows.Count

Function ListRowCount(ByVal FirstCellName as String) as Long
With thisworkbook.Names(FirstCellName).RefersToRange
If isempty(.Offset(1,0).value) Then
ListRowCount = 1
Else
ListRowCount = .End(xlDown).row - .row + 1
End If
End With
End Function
But if you are damn sure there's nothing around the list, then just thisworkbook.Names(FirstCellName).RefersToRange.CurrentRegion.rows.count

Why not use an Excel formula to determine the rows? For instance, if you are looking for how many cells contain data in Column A use this:
=COUNTIFS(A:A,"<>")
You can replace <> with any value to get how many rows have that value in it.
=COUNTIFS(A:A,"2008")
This can be used for finding filled cells in a row too.

You can also use:
Range( RangeName ).end(xlDown).row
to find the last row with data in it starting at your named range.

I am sure that you probably wanted the answer that #GSerg gave. There is also a worksheet function called rows that will give you the number of rows.
So, if you have a named data range called Data that has 7 rows, then =ROWS(Data) will show 7 in that cell.

That single last line worked perfectly #GSerg.
The other function was what I had been working on but I don't like having to resort to UDF's unless absolutely necessary.
I had been trying a combination of excel and vba and had got this to work - but its clunky compared with your answer.
strArea = Sheets("Oper St Report CC").Range("cc_rev").CurrentRegion.Address
cc_rev_rows = "=ROWS(" & strArea & ")"
Range("cc_rev_count").Formula = cc_rev_rows

Related

Fill a table with input from other table via VBA

I am trying to fill a table in one sheet with input from another table (different sheet "wsQuant") based on this function:
=CountIfs(Tabelle14[Question1];[#[Q1]];Tabelle14[Name];[#[Name]];Tabelle14[Participation];[#[Participation]])
This works, but since the table is very big, I am trying to realize it via VBA since the file always crashes (I assume it's too much calculation at once). My solution so far is:
Sub DataBase()
Dim wsQuant As Worksheet
Set wsQuant = Worksheets("quantitativ")
Range("I5").Value = WorksheetFunction.CountIfs("wsQuant[$Z$4:$Z$1000]", [#[Q1]], "wsQuant[Name]", [#[Name]], "wsQuant[Participation]", [#[Participation]])
End Sub
This doesn't work and I can't figure out why.
I searched the entire day now and didn't find any answers.
Any help would be gratly appreciated.
EDIT:
Sample:
Data from the questionaire
This is how the data is given. Answers to the questions on a scale from 1 to 5.
List to be filled
This list should be filled through counting the answers that match the first 4 cloumns.

PasteSpecial not working - VBA [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to copy the data from one worksheet and paste it in another worksheet. But it is not working and asking me to use "A1" or R1C1. But I need to start the pasting from cell A5.
This is the code -
Range("A5:C9").PasteSpecial
Kindly share your thoughts. Thanks.
If you just need to paste the data from one worksheet to another, you can skip PasteSpecial (well, .Copy altogether) and just set the two ranges equal to another.
Worksheets("DESTworksheet").Range([DESTINATION range]).Value = Worksheets("ORIGINworksheet").Range([COPY range]).Value
So, try:
Worksheets("DestinationSheet").Range("A5:C9").Value = Worksheets("CopyFromSheet").Range("A5:C9").Value
Of course changing the worksheet names (and/or ranges) as necessary.

Excel VBA - If Then Replace [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am very new to VBA and I have read so much on the If-Then-Replace that I think I'm confusing myself. Within my worksheet I have two columns that come in with more data than what I need and I have to get it pared down in order to concatenate it.
Column H (Header) has the following data in each cell: Network: Series: Episode : Data
Column J (Header) has the following data in each cell: Network: Type: Type2: Type3
I run a text-to-columns to get it down to have the Network in 1 column and the Series in the 2nd column.
What I need is a Search in Column J for "specific network". If this is true then replace the network in Column H.
Question: Will I need to also run a text-to-columns in Column J to get a 1:1 comparison?
Question: Can you help me with the VBA code to do the Find-If-Then-Replace?
Thank you so much!
This should do what you're looking to do... If it works for you, please mark the answer as accepted by clicking the checkmark to the left.
Sub Freplace()
dim i as int
dim s as string
i = 1
s = "specific network"
Do until Cells(i,10).Value = ""
If Cells(i,10).Value = s Then Cells(i,8).Value = s
i = i+1
Loop
End Sub

Automatic excel cell fill up from another sheet [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Here's the situation:
I have two excel sheets.
The first sheet contains a table of product codes and product descriptions (Two columns A and B).
I have a second sheet where someone is supposed to enter the product code and automatically have the next cell fill up with the product description and the cell next to it with the time.
I was wondering if that's do-able without VBA? If so, can someone give me start.
Best,
You could have the first cell that you want to auto fill have an IF statement, where if the cell has no value, nothing happens, and anything other than that gets a calculation.
Using A2:B100 as a Range if you have a header Row. Adjust to your own needs, of course.
IF FUNCTION
'IF (condition, result if true, result if false)'
VLOOKUP FUNCTION
'VLOOKUP(lookup_value,table_array,col_index_num,range_lookup)'
=If($A1 = "", "", VLOOKUP(A1,Sheet1!$A$2:$B$100,2))
The next cell over would have something similar:
=IF($A1 = "", "", NOW())
This will get you the time. You will also have to have the cell format set to Time.
There is a problem with that. The screenshot below illustrates it.
It will just keep refreshing with the current time over and over. I would use a bit of VBA to solve that by setting the value property instead of a formula.
Sheets("Sheet2").Cells(row,col).Value = Now()
You could copy and paste the value into another cell. Just the value. Not the formula.
Or you could check out this article: about generating Time Stamps.
edit: included VBA solution

removing characters from a text in excel using vba [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to manipulate a text in excel using vba. The text I am tyring to manipuate look like the following two texts:
text#1: ...54
text #2: ..2
I am trying to remove the dots from these texts so that only the number(s) remains, as follow54
text#1: 54
text#2: 2
Please advise.
This is how it works:
Sub ReplaceDots()
Dim col as Integer
col = 2 'The Column where you want to replace the dots
For Row = 1 To 20 'Define the Range here
Set curCell = Worksheets("Sheet1").Cells(Row, col)
curCel.Value = Replace(curCel.Value, ".", "")
Next Row
End Sub
edit: maybe i switched rows and columns, you have to test
Have a look at the Replace function.
Replace ( string1, find, replacement, [start, [count, [compare]]] )
string1 is the string to replace a sequence of characters with another set of characters.
find is the string that will be searched for in string1.
replacement will replace find in string1.
start is optional. This is the position in string1 to begin the search. If this parameter is omitted, the REPLACE function will begin the search at position 1.
count is optional. This is the number of occurrences to replace. If this parameter is omitted, the REPLACE function will replace all occurrences of find with replacement.
compare is optional. This can be one of the following values:
vbBinaryCompare Binary comparison
vbTextCompare Textual comparison
vbDatabaseCompare Performs a comparison based on information in your database
http://www.techonthenet.com/excel/formulas/replace_vba.php