Good Morning
I have a program in VB.Net that exports a file from Datagridview into Excel file
and it looks like this.
My goal here is how can I lock some columns? based on the Image above? Lock all columns except the column that has a color yellow? I mean all the columns except the yellow are uneditable.
Here is my code in exporting excel
Try
If DataGridView1.Rows.Count = 0 Then
MsgBox("Nothing to Export")
Else
Dim ExcelApp As Object, ExcelBook As Object
Dim ExcelSheet As Object
Dim i As Integer
Dim J As Integer
Dim rowIndex As Integer = 1
Dim total As Double = 0
Dim indexTotal As Integer
ExcelApp = CreateObject("Excel.Application")
ExcelBook = ExcelApp.WorkBooks.Add
ExcelSheet = ExcelBook.WorkSheets(1)
With ExcelSheet
rowIndex += 2
For Each column As DataGridViewColumn In DataGridView1.Columns
.cells(rowIndex, column.Index + 1) = column.HeaderText
Next
.Range(.Cells(rowIndex, 1), .Cells(rowIndex, DataGridView1.Columns.Count)).Font.Bold = True
rowIndex += 1
For i = 0 To Me.DataGridView1.RowCount - 1
.cells(rowIndex, 1) = Me.DataGridView1.Rows(i).Cells("ItemCode").Value
For J = 1 To DataGridView1.Columns.Count - 1
If IsNumeric(DataGridView1.Rows(i).Cells(J).Value) Then
.cells(rowIndex, J + 1).NumberFormat = "#,##0.00"
.cells(rowIndex, J + 1) = DataGridView1.Rows(i).Cells(J).Value
Else
.cells(rowIndex, J + 1) = DataGridView1.Rows(i).Cells(J).Value
End If
'You can test also by index for example : if J = indexofTotalColumn then
If DataGridView1.Columns(J).Name = "Total" Then
total += DataGridView1.Rows(i).Cells(J).Value
indexTotal = J
End If
Next
rowIndex += 1
.Columns("A:Z").EntireColumn.AutoFit()
.Columns("L").ColumnWidth = 0
.cells(5).Locked = False
Next
.Protect("fakepwd")
End With
ExcelApp.Visible = True
ExcelSheet = Nothing
ExcelBook = Nothing
ExcelApp = Nothing
End If
Catch
End Try
TYSM for help
Set the Locked property of the cell to false, where J+1 is the desired column number.
For example to unlock column 5 :
For J = 1 To DataGridView1.Columns.Count - 1
If J=5 then
.cells(rowIndex, J + 1).Locked=False
End if
If IsNumeric(DataGridView1.Rows(i).Cells(J).Value) Then
..........
In the code, once you are done populating data in sheet, protect the sheet
Next
.Protect ("fakepwd")
End With
Related
I have two sheets data and PrevErrCheck. I am checking all occurrence of variable VarVal(this variable has data in E1 cell of PrevErrCheck) in sheet data and copy entire row to sheet PrevErrCheck. But the problem I am facing here is running macro multiple times overwriting data. I would like to keep the copied rows in sheet data and whenever I run next time, it should copy to next blank row.
I am using below code currently but bit confused to how to integrate the the option to find last row on PrevErrCheck and copy lines below that
Sub PrevErrCheck()
Dim spem As Workbook
Dim PrevErrCheck As Worksheet
Dim data As Worksheet
Dim xRg As Range
Dim xCell As Range
Dim I As Long
Dim J As Long
Dim K As Long
Set spem = Excel.Workbooks("SwitchPortErrorMonitor.xlsm")
Set PrevErrCheck = spem.Worksheets("PrevErrCheck")
Set data = spem.Worksheets("data")
spem.Worksheets("PrevErrCheck").Activate
VarVal = PrevErrCheck.Cells(1, "E").Value
I = data.UsedRange.Rows.count
J = PrevErrCheck.UsedRange.Rows.count
If J = 1 Then
If Application.WorksheetFunction.CountA(PrevErrCheck.UsedRange) = 0 Then J = 0
End If
Set xRg = data.Range("X:X")
On Error Resume Next
Application.ScreenUpdating = False
J = 3
For K = 1 To xRg.count
If CStr(xRg(K).Value) = VarVal And Not IsEmpty(VarVal) Then
xRg(K).EntireRow.Copy Destination:=PrevErrCheck.Range("A" & J + 1)
PrevErrCheck.Range("X" & J + 1).ClearContents
J = J + 1
End If
Next
Application.ScreenUpdating = True
End Sub
You have J = 3 before the loop, that may be a problem. xRg.count always returns 1048576, you should use something more specific. Try this:
Set spem = Excel.Workbooks("SwitchPortErrorMonitor.xlsm")
Set PrevErrCheck = spem.Worksheets("PrevErrCheck")
VarVal = PrevErrCheck.Cells(1, "E").Value
If IsEmpty(VarVal) Then Exit Sub
Set data = spem.Worksheets("data")
spem.Worksheets("PrevErrCheck").Activate
I = data.UsedRange.Rows.Count
J = PrevErrCheck.UsedRange.Rows.Count + 1
If J = 2 Then
If IsEmpty(PrevErrCheck.Cells(1, 1)) Then J = 1
End If
' If J = 1 Then
' If Application.WorksheetFunction.CountA(PrevErrCheck.UsedRange) = 0 Then J = 0
' End If
' Set xRg = data.Range("X:X")
' On Error Resume Next
' Application.ScreenUpdating = False
' J = 3
For K = 1 To I
If CStr(data.Cells(K, "X").Value) = VarVal Then
data.Cells(K, 1).EntireRow.Copy Destination:=PrevErrCheck.Range("A" & J)
PrevErrCheck.Range("X" & J).ClearContents
J = J + 1
End If
Next
' Application.ScreenUpdating = True
End Sub
I have a data grid view , I want to export to Excel.
I want to export only the Visible columns in the data grid view.
But I keep getting this error.
Private Sub btnExport_Click(sender As Object, e As EventArgs) Handles btnExport.Click
Dim ExcelApp As Excel.Application
Dim ExcelWorkBk As Excel.Workbook
Dim ExcelWorkSht As Excel.Worksheet
Dim i As Integer
Dim j As Integer
ExcelApp = New Excel.Application
ExcelWorkBk = ExcelApp.Workbooks.Add()
ExcelWorkSht = ExcelWorkBk.Sheets("Sheet1")
Dim columnsCount As Integer = DGVinfo3.Columns.Count
For i = 0 To DGVinfo3.RowCount - 1
If DGVinfo3.Columns(i).Visible = True Then
For j = 0 To DGVinfo3.ColumnCount - 1
For k As Integer = 0 To DGVinfo3.Columns.Count + 1
If DGVinfo3.Columns(k).Visible = True Then
ExcelWorkSht.Cells(1, k) = DGVinfo3.Columns(k - 1).HeaderText
ExcelWorkSht.Cells(1, k).Font.Bold = True
ExcelWorkSht.Cells(1, k).interior.color = RGB(192, 203, 219)
ExcelWorkSht.Cells(i + 1, j + 1) = DGVinfo3(j, i).Value
End If
Next
Next
End If
Next
End Sub
I keep getting this error:
System. Argument Out Of Range Exception: 'Index was out of range. Must be
non-negative and less than the size of the collection.'
Here is where I get the Error:
ExcelWorkSht.Cells(1, k) = DGVinfo3.Columns(k - 1).HeaderText
Just loop the rows and inside that loop each column. Also because the hidden columns won't be exported to Excel, it's probably best to keep track of the Excel columns in a separate variable:
Dim xlColumn As Integer
For i = 0 To DGVinfo3.RowCount - 1
xlColumn = 0
For j = 0 To DGVinfo3.ColumnCount - 1
If DGVinfo3.Columns(j).Visible = True Then
xlColumn += 1
If i = 0 Then
'You only need to set the column headers for the first row
ExcelWorkSht.Cells(1, xlColumn) = DGVinfo3.Columns(j).HeaderText
ExcelWorkSht.Cells(1, xlColumn).Font.Bold = True
ExcelWorkSht.Cells(1, xlColumn).interior.color = RGB(192, 203, 219)
End If
'i + 2 because the header is row 1
ExcelWorkSht.Cells(i + 2, xlColumn) = DGVinfo3(i, j).Value
End If
Next
Next
I have an application that can retrieves the install date and save it at a datagridview column with the format of date only.
But when I tried to export it to an excel file, even when I tried to format the cell, it still shows me datetime instead of date only for some of the data.
The code for exporting is shown below. btw I'm using vb.net
'reportFile : True = IE_Version_Report.xlsx False = Data.xlsx
Sub ExportData(reportFile)
Dim dSet As New DataSet
dSet.Tables.Add()
For i As Integer = 0 To DataGridView1.ColumnCount - 1
dSet.Tables(0).Columns.Add(DataGridView1.Columns(i).HeaderText)
Next
Dim dr As DataRow
For i As Integer = 0 To DataGridView1.RowCount - 1
dr = dSet.Tables(0).NewRow
For j As Integer = 0 To DataGridView1.Columns.Count - 1
dr(j) = DataGridView1.Rows(i).Cells(j).Value
Next
dSet.Tables(0).Rows.Add(dr)
Next
Dim Ex As Microsoft.Office.Interop.Excel.Application
Dim Wb As Microsoft.Office.Interop.Excel.Workbook
Dim Ws As Microsoft.Office.Interop.Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Ex = New Microsoft.Office.Interop.Excel.Application
Wb = Ex.Workbooks.Add(misValue)
Ws = Wb.Sheets("sheet1")
Dim dt As DataTable = dSet.Tables(0)
Dim col, row As Integer
Dim rawData(dt.Rows.Count, dt.Columns.Count - 1) As Object
For col = 0 To dt.Columns.Count - 1
rawData(0, col) = dt.Columns(col).ColumnName.ToUpper
Next
For col = 0 To dt.Columns.Count - 1
For row = 0 To dt.Rows.Count - 1
rawData(row + 1, col) = dt.Rows(row).ItemArray(col)
Next
Next
Dim finalColLetter As String = String.Empty
finalColLetter = ExcelColName(dt.Columns.Count)
Dim excelRange As String = String.Format("A1:{0}{1}", finalColLetter, dt.Rows.Count + 1)
Ws.Range(excelRange, Type.Missing).Value2 = rawData
Ws.Range("A1:L1").EntireColumn.AutoFit() 'columns in excel file will autofit according to the data
Dim num As Integer = DataGridView1.Rows.Count + 1
'set the format for dates in these few columns
Ws.Range("D2:D" & num).NumberFormat = "dd/mm/yyyy;#"
Ws.Range("F2:F" & num).NumberFormat = "dd/mm/yyyy;#"
Ws.Range("H2:H" & num).NumberFormat = "dd/mm/yyyy;#"
Ws.Range("J2:J" & num).NumberFormat = "dd/mm/yyyy;#"
Ws.Range("L2:L" & num).NumberFormat = "dd/mm/yyyy;#"
Ws = Nothing
If reportFile = True Then
fileExported = True
If System.IO.File.Exists(FolderPath & "\MSOffice_Report.xlsx") Then
System.IO.File.Delete(FolderPath & "\MSOffice_Report.xlsx")
End If
Wb.SaveAs(FolderPath & "\MSOffice_Report.xlsx")
Else
fileExported = False
If System.IO.File.Exists("C:\Install\MSData.xlsx") Then
System.IO.File.Delete("C:\Install\MSData.xlsx")
End If
Wb.SaveAs("C:\Install\MSData.xlsx")
SetAttr("C:\Install\MSData.xlsx", vbHidden)
End If
Wb.Close(True)
Wb = Nothing
Ex.Quit()
Ex = Nothing
GC.Collect()
End Sub
Public Function ExcelColName(ByVal Col As Integer) As String
If Col < 0 And Col > 256 Then
MsgBox("Invalid Argument", MsgBoxStyle.Critical)
Return Nothing
Exit Function
End If
Dim i As Int16
Dim r As Int16
Dim S As String
If Col <= 26 Then
S = Chr(Col + 64)
Else
r = Col Mod 26
i = System.Math.Floor(Col / 26)
If r = 0 Then
r = 26
i = i - 1
End If
S = Chr(i + 64) & Chr(r + 64)
End If
ExcelColName = S
End Function
Sorry if it's a very stupid question but I really don't know what's wrong with it.
Thanks!
Excel does not recognise dates earlier than 1/1/1900 and will treat a value like 11/11/1111 12:00:00 AM as text. See how the dates are right-aligned and the text is left aligned in your screenshot?
Text will not be affected by the number formatting you apply to show only the date of the values.
Depending on what you want to achieve, you need to adjust your code to handle the text values differently. Replace them with 1/1/1900 or some such.
Good Afternoon to all
I populate data in Datagridview from mySQL like this.
Datagridview Data
the next thing I do is that I have an Export Button and if I Click that it will Export the Data from Datagridview in Excel like this
Extracted in Excel
My Question is How can I Find the Last Data in Column "Total" and Put the Sum below that? As of now the Image shows only two rows in excel but someday it will populate, I just want to Sum up all the data in the Column "Total" and Display the Output in below the last Data. I hope you help me. :(
TYSM
by the way here is my code
If DataGridView1.Rows.Count = 0 Then
MsgBox("Nothing to Export")
Else
Dim ExcelApp As Object, ExcelBook As Object
Dim ExcelSheet As Object
Dim i As Integer
Dim j As Integer
Dim exl As Excel.Application
Dim NewWorksheet As Microsoft.Office.Interop.Excel.Worksheet
Dim myRange As Microsoft.Office.Interop.Excel.Range
ExcelApp = CreateObject("Excel.Application")
ExcelBook = ExcelApp.WorkBooks.Add
ExcelSheet = ExcelBook.WorkSheets(1)
exl = New Excel.Application
exl.Visible = True
With ExcelSheet
For Each column As DataGridViewColumn In DataGridView1.Columns
.cells(1, column.Index + 1) = column.HeaderText
Next
For i = 1 To Me.DataGridView1.RowCount
.cells(i + 1, 1) = Me.DataGridView1.Rows(i - 1).Cells("ItemCode").Value
For j = 1 To DataGridView1.Columns.Count - 1
.cells(i + 1, j + 1) = DataGridView1.Rows(i - 1).Cells(j).Value
.Cells(4, 13) = "Grand Total"
.Cells(4, 14).Formula = "=SUM(Sheet1!$J2:$J1048576)"
Next
Next
End With
NewWorksheet = DirectCast(ExcelBook.Sheets(1), Microsoft.Office.Interop.Excel.Worksheet)
myRange = NewWorksheet.Range("A:K")
myRange.Font.Bold = True
myRange.Font.Size = 9
myRange.Font.FontStyle = "Calibri"
ExcelSheet.Rows.Item(1).EntireColumn.AutoFit()
ExcelApp.Visible = True
ExcelSheet = Nothing
ExcelBook = Nothing
ExcelApp = Nothing
End If
Excel coordinates work by (column, row)
Dim rowIndex As Integer = 1
For i = 1 To Me.DataGridView1.RowCount
.cells(1, i + 1) = Me.DataGridView1.Rows(i - 1).Cells("ItemCode").Value
rowIndex += 1
For j = 1 To DataGridView1.Columns.Count - 1
.cells(j + 1, i + 1) = DataGridView1.Rows(i - 1).Cells(j).Value
Next
Next
.Cells(9, rowIndex + 1) = "Grand Total"
.Cells(10, rowIndex + 1).Formula = "=SUM(Sheet1!J2:J" & rowIndex.ToString().Trim() & ")"
Hello Everyone Good Morning.
I have a Program in VB.Net that will Populate Data from Mysql into the Datagridview.
I have also a button called Export and It will Export Datagridview Data in Excel Format like this.
But my our Prof. likes this Format.
How can I achieve this?
Put a Center Header
Put a .00 at the End of the Number of a Number Column
Find the Last Cell in a Column and Sum It.
I hope someone would help me.
Here is my code in Export
If DataGridView1.Rows.Count = 0 Then
MsgBox("Nothing to Export")
Else
Dim ExcelApp As Object, ExcelBook As Object
Dim ExcelSheet As Object
Dim i As Integer
Dim j As Integer
ExcelApp = CreateObject("Excel.Application")
ExcelBook = ExcelApp.WorkBooks.Add
ExcelSheet = ExcelBook.WorkSheets(1)
With ExcelSheet
For Each column As DataGridViewColumn In DataGridView1.Columns
.cells(1, column.Index + 1) = column.HeaderText
Next
For i = 1 To Me.DataGridView1.RowCount
.cells(i + 1, 1) = Me.DataGridView1.Rows(i - 1).Cells("ItemCode").Value
For j = 1 To DataGridView1.Columns.Count - 1
.cells(i + 1, j + 1) = DataGridView1.Rows(i - 1).Cells(j).Value
Next
Next
End With
ExcelApp.Visible = True
ExcelSheet = Nothing
ExcelBook = Nothing
ExcelApp = Nothing
End If
Try this code :
If DataGridView1.Rows.Count = 0 Then
MsgBox("Nothing to Export")
Else
Dim ExcelApp As Object, ExcelBook As Object
Dim ExcelSheet As Object
Dim i As Integer
Dim j As Integer
Dim rowIndex As Integer = 1
Dim total As Double = 0
Dim indexTotal As Integer
ExcelApp = CreateObject("Excel.Application")
ExcelBook = ExcelApp.WorkBooks.Add
ExcelSheet = ExcelBook.WorkSheets(1)
With ExcelSheet
With .Range(.Cells(rowIndex, 1), .Cells(rowIndex, DataGridView1.Columns.Count))
.HorizontalAlignment = Excel.Constants.xlCenter
.VerticalAlignment = Excel.Constants.xlCenter
.MergeCells = True
.Value = "PURCHASE REQUISITION"
.Font.Bold = True
End With
rowIndex += 2
For Each column As DataGridViewColumn In DataGridView1.Columns
.cells(rowIndex, column.Index + 1) = column.HeaderText
Next
.Range(.Cells(rowIndex, 1), .Cells(rowIndex, DataGridView1.Columns.Count)).Font.Bold = True
rowIndex += 1
For i = 0 To Me.DataGridView1.RowCount - 1
.cells(rowIndex, 1) = Me.DataGridView1.Rows(i).Cells("ItemCode").Value
For j = 1 To DataGridView1.Columns.Count - 1
If IsNumeric(DataGridView1.Rows(i).Cells(j).Value) Then
.cells(rowIndex, j + 1).NumberFormat = "#,##0.00"
.cells(rowIndex, j + 1) = DataGridView1.Rows(i).Cells(j).Value
Else
.cells(rowIndex, j + 1) = DataGridView1.Rows(i).Cells(j).Value
End If
'You can test also by index for example : if j = indexofTotalColumn then
If DataGridView1.Columns(j).Name = "Total" Then
total += DataGridView1.Rows(i).Cells(j).Value
indexTotal = j
End If
Next
rowIndex += 1
Next
.cells(rowIndex, indexTotal) = "Grand Total"
.cells(rowIndex, indexTotal + 1).NumberFormat = "#,##0.00"
.cells(rowIndex, indexTotal + 1) = total
.Range(.Cells(rowIndex, indexTotal), .Cells(rowIndex, indexTotal + 1)).Font.Bold = True
End With
ExcelApp.Visible = True
ExcelSheet = Nothing
ExcelBook = Nothing
ExcelApp = Nothing
End If