I don't have any error while running the below codes but its not print the result in the destination sheet. its just print 0 only
object workbook sheet is have data and I'm using the specific column status with conditions
Sub Counts()
Dim objWorkbook As Workbook
'open the workbook with data
Set objWorkbook = Workbooks.Open("H:\TEST\July 2019 Tracker - Break Down & Travels.xlsx")
Dim i As Long
Dim GGV As Integer
GGV = 0
objWorkbook.Activate
For i = 1 To 1000
If Sheet1.Cells(i, 20) = "GSC" And Sheet1.Cells(i, 21) = "Travel" Then
GGV = GGV + 1
End If
Next i
'this work book
Sheet4.Cells(6, 2) = GGV
objWorkbook.Close
End Sub
wanted to print the count value in this workbook
Related
I am Using Macro for pivot table in Excel...
when i am trying to put pivot table for a data of around 80,000 rows my pivot table is not appearing but if i take only 65000 rows than its working can anyone help me in debugging this error.
These are the VBA Codes that i am using
Option Explicit
Sub Macro1()
'Declare Variables
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
'Delete Preivous Pivot Table Worksheet & Insert a New Blank Worksheet With Same Name
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("PivotTable").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "PivotTable"
Application.DisplayAlerts = True
Set PSheet = Worksheets("PivotTable")
Set DSheet = Worksheets("Data")
'Define Data Range
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
'Define Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:="EmployeePivotTable")
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="EmployeePivotTable")
'Insert Row Fields
With ActiveSheet.PivotTables("EmployeePivotTable").PivotFields("FilePeriod")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("EmployeePivotTable").PivotFields("ProjectNumber")
.Orientation = xlRowField
.Position = 2
End With
'Insert Column Fields
With ActiveSheet.PivotTables("EmployeePivotTable").PivotFields("EffectiveFTE")
.Orientation = xlColumnField
.Position = 1
End With
'Format Pivot Table
ActiveSheet.PivotTables("EmployeePivotTable").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("EmployeePivotTable").TableStyle2 = "PivotStyleMedium9"
End Sub
I have a problem with a XLSM Excel of which I want to get some data by condition.
Excel image
In the picture I have "Name", and "Status", if the status is "Completed" I want to take their value on "Time Assembly -> Post Assembly" where the value is "1.11". For each value of "Time Assembly" in the end I want to do media.
my code:
If FileUpload1.HasFile = False Then
Exit Sub
Else
Dim FileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim Extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName)
'Dim FolderPath As String = ConfigurationManager.AppSettings("Upload")
Dim FilePath As String = Server.MapPath("~") & "\Upload\" & FileName
FileUpload1.SaveAs(FilePath)
Dim existingFile = New FileInfo(FilePath)
Dim pack As ExcelPackage = New ExcelPackage(existingFile)
Dim workBook As ExcelWorkbook = pack.Workbook
If workBook.Worksheets.Count > 0 Then
Dim currentWorksheet As ExcelWorksheet = workBook.Worksheets.First()
End If
End If
In that "if" I want to read Excel row by row, after that condition.
Thanks a lot!
Try to find the last row in the sheet and use for loop to iterate the condition
Sub test()
Dim lastRow As Long
Dim workBook As workBook
workBook = ThisWorkbook
lastRow = workBook.ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
For i = 3 To lastRow
'for checking value in the 4 column(D column)
If (workBook.ActiveSheet.Cells(i, 4).Value) = "completed" Then
workBook.ActiveSheet.Cells(i, 6).Value = "xx"
End If
'for adding value in the 6 column
Next
End Sub
I have been able to create simple word document using OpenXML. I am trying to add multi-level numbered list as follows:
1. Point 1
a. Sub Point 1
I have tried following code
Step 1:
Dim numberid As New NumberingId
numberid.Val = 1
Dim numlevelref As New NumberingLevelReference
numlevelref.Val = 0
Dim numberprop As New NumberingProperties
numberprop.NumberingId = numberid
numberprop.NumberingLevelReference = numlevelref
Dim pp As New ParagraphProperties
pp.NumberingProperties = numberprop
Dim p As Paragraph = New Paragraph
p.Append(pp)
Dim run As New Run
run.Append(New Text("Point 1"))
p.Append(run)
Step 2:
Dim numberid As New NumberingId
numberid.Val = 1
Dim numlevelref As New NumberingLevelReference
numlevelref.Val = 1 '(Only this has changed from Step 1)
Dim numberprop As New NumberingProperties
numberprop.NumberingId = numberid
numberprop.NumberingLevelReference = numlevelref
Dim pp As New ParagraphProperties
pp.NumberingProperties = numberprop
Dim p As Paragraph = New Paragraph
p.Append(pp)
Dim run As New Run
run.Append(New Text("Sub Point 1"))
p.Append(run)
This gives me following output:
1. Point 1
1. Sub Point 1
Please let me know how to apply number format. I have understood following is the class to use:
Dim numformat As New NumberingFormat
numformat.Val = NumberFormatValues.LowerLetter
But where to use numformat in Step 2?
I am using a csvReader in order to retrieve data from a csv file. My csv file consists of 500elements and I only need the first 300. How do I limit my csvReader in order to return only 300 elements?
Dim PressureTable As DataTable = GetDataTabletFromCSVFile(BackUpDirDminus1)
Console.WriteLine("Rows count:" + PressureTable.Rows.Count.ToString)
Console.ReadLine()
Using CSVReader As New TextFieldParser(BackUpDirDminus1)
CSVReader.SetDelimiters(New String() {","})
CSVReader.HasFieldsEnclosedInQuotes = True
'read column names
Dim colFields As String() = CSVReader.ReadFields()
'For Each column As String In colFields
While Not CSVReader.EndOfData
Dim fieldData As String() = CSVReader.ReadFields()
'Making empty value as null
For i As Integer = 0 To fieldData.Length-1
If fieldData(i) = "" Then
fieldData(i) = Nothing
End If
Next
PressureTable.Rows.Add(fieldData)
End While
End Using
Please help. Thanks
I suppose there should be a method name "ReadNextRecord()", so your while loop should be like
While CSVReader.ReadNextRecord()
Declare a int k =0
and do k++
Once K++ reaches 300, then you can End While.
In my ASP.NET application, i need to read the CSV file and insert into sql server.
and it was strange that it treat the 1st row (column name) as LineNumber=2.
but i found that my code cannot read the last row of the CSV file.
Dim CSV_content As String = ""
Dim CSVFilePathName As String = Server.MapPath("~/XXX/" & filename)
If File.Exists(CSVFilePathName) = True Then
'Response.Write("exists")
Dim afile As FileIO.TextFieldParser = New FileIO.TextFieldParser(CSVFilePathName)
Dim CurrentRecord As String()
afile.TextFieldType = FileIO.FieldType.Delimited
afile.Delimiters = New String() {","}
afile.HasFieldsEnclosedInQuotes = True
Dim LastName As String = ""
Dim FirstName As String = ""
Dim DisplayName As String = ""
Dim EmailName As String = ""
Dim dc As New dcHRISDataContext
' parse the actual file
Do While Not afile.EndOfData
Try
CurrentRecord = afile.ReadFields
'insert into tmp db
If afile.LineNumber > 2 Then
Dim newRecord1 As New XXX
dc.XXX.InsertOnSubmit(newRecord1)
newRecord1.LastName = CurrentRecord(0).Trim
newRecord1.FirstName = CurrentRecord(1).Trim
newRecord1.DisplayName = CurrentRecord(1).Trim
newRecord1.DirEmail = CurrentRecord(3).Trim
newRecord1.GetFileDate = DateTime.Now
newRecord1.GetFileName = filename
dc.SubmitChanges()
End If
Catch ex As FileIO.MalformedLineException
Stop
End Try
Loop
End If
Wow, this is the dumbest issue that I have seen.
Anyhow, Apparently the last row in the TextFieldParser is -1, and not LastRowWithValuesIndex + 1. So what happens is this.
You read the data of the last line (this is fine)
The reader skips to the next line to prepare the next line to read (this is fine)
You get the afile.LineNumber, now set to the next line. Which is -1
at this point. (this is dumb although understandable, not your fault)
Your if statement wont let you in because hey you are on line -1.
Loop ends, skipping last row in the CSV.
All you have to do is to read the line number of the row before reading the data:
Dim i As Integer = afile.LineNumber
CurrentRecord = afile.ReadFields
If i > 2 Then
etc