Bana excel vba ile bir kod yaz. Bu kod "ONCELIK" sayfasında "A" sütununda kaç satır…
- User
Bana excel vba ile bir kod yaz. Bu kod "ONCELIK" sayfasında "A" sütununda kaç satır olduğunu bulsun. Sonra bir döngü başlatsın. Bu döngü "A" sütunundaki satır sayısına ulaşana kadar devam etsin. Döngünün içinde şunu yapsın: "F" Sütunundaki ilk boş hücreyi bulup bulduğu hücrenin bir üst hücresindeki değeri her hücrede +1 arttırarak bir sonraki dolu hücreye kadar uygulamalı. Dolu hücreye ulaştığında sütundaki bir sonraki boş hücreyi bulup bulduğu hücrenin üst hücresindeki değeri her hücrede +1 arttırarak bir sonraki dolu hücreye kadar uygulamalı. Döngü "A" sütunundaki satır sayısına ulaşınca bitsin.
- Assistant
Aşağıda istediğiniz gibi çalışan Excel VBA kodunu bulabilirsiniz:
Sub öncelik_döngü() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("ONCELIK") Dim satir_sayisi As Integer satir_sayisi = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row Dim i As Integer Dim bos_hucre As Range Dim dolu_hucre As Range For i = 1 To satir_sayisi If ws.Cells(i, "F") = "" Then If bos_hucre Is Nothing Then Set bos_hucre = ws.Cells(i, "F") End If Else If Not bos_hucre Is Nothing Then Set dolu_hucre = ws.Cells(i, "F") ws.Range(bos_hucre, dolu_hucre.Offset(-1, 0)).Value = dolu_hucre.Offset(-1, 0).Value + 1 Set bos_hucre = Nothing End If End If Next i If Not bos_hucre Is Nothing Then ws.Range(bos_hucre, ws.Cells(satir_sayisi, "F")).Value = bos_hucre.Offset(-1, 0).Value + 1 End If End SubBu kodu, istediğiniz Excel dosyasındaki VBA modülüne yapıştırarak kullanabilirsiniz. Öncelik sayfasındaki A sütunundaki satır sayısını belirler ve F sütunundaki boş hücreleri doldurmak için bir döngü başlatarak, boş hücreleri ve bir sonraki dolu hücreyi bulur ve belirtilen şekilde değerler ekler.
- User
Hücreleri dolduruyor fakat aynı değerleri yazıyor. Her satırda +1 arttırsın.
- Assistant
Özür dilerim, anladım. İşte her satıra +1 artarak değer ekleyen düzeltilmiş VBA kodu.
Sub öncelik_döngü() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("ONCELIK") Dim satir_sayisi As Integer satir_sayisi = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row Dim i As Integer Dim bos_hucre As Range Dim dolu_hucre As Range Dim increment As Integer For i = 1 To satir_sayisi If ws.Cells(i, "F") = "" Then If bos_hucre Is Nothing Then Set bos_hucre = ws.Cells(i, "F") End If Else If Not bos_hucre Is Nothing Then Set dolu_hucre = ws.Cells(i, "F") increment = 1 For Each cell In ws.Range(bos_hucre, dolu_hucre.Offset(-1, 0)) cell.Value = dolu_hucre.Offset(-1, 0).Value + increment increment = increment + 1 Next cell Set bos_hucre = Nothing End If End If Next i If Not bos_hucre Is Nothing Then increment = 1 For Each cell In ws.Range(bos_hucre, ws.Cells(satir_sayisi, "F")) cell.Value = bos_hucre.Offset(-1, 0).Value + increment increment = increment + 1 Next cell End If End Sub - User
Hala bir sonraki dolu hücreye kadar olan bütün boş hücrelere aynı değer yazılıyor. Bir sonraki boş hücreye kadar her hücre geçişinde +1 arttırılmalı.
- Assistant
Anladım, kusura bakmayın. İşte her hücre geçişinde +1 arttıran düzeltilmiş VBA kodu:
Sub öncelik_döngü() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("ONCELIK") Dim satir_sayisi As Integer satir_sayisi = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row Dim i As Integer Dim bos_hucre As Range Dim dolu_hucre As Range Dim increment As Integer For i = 1 To satir_sayisi If ws.Cells(i, "F") = "" Then If bos_hucre Is Nothing Then Set bos_hucre = ws.Cells(i, "F") Else ws.Cells(i, "F").Value = bos_hucre.Offset(-1, 0).Value + 1 End If Else If Not bos_hucre Is Nothing Then Set dolu_hucre = ws.Cells(i, "F") ws.Cells(bos_hucre.Row, "F").Value = dolu_hucre.Offset(-1, 0).Value + 1 Set bos_hucre = Nothing End If End If Next i End SubBu kod, boş hücrelerin her biri için bir önceki dolu hücreye göre +1 artmış değere sahip olarak her hücre geçişinde değeri arttıracaktır. Bu sayede her boş hücre için değer +1 artarak yazılacaktır.
- User
Son yazdığın kod olmadı. dolu hücreden sonraki hücreyi 2 arttırdı. Bunu istemiyorum.
- Assistant
Özür dilerim, anlaşılan yanlış anladım. Dolu hücrelerin son artıştan başlayarak değil, her seferinde +1 artarak işlenmesini istiyorsunuz. İşte yeni düzeltilmiş VBA kodu:
Sub öncelik_döngü() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("ONCELIK") Dim satir_sayisi As Integer satir_sayisi = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row Dim i As Integer Dim bos_hucre As Range Dim dolu_hucre As Range Dim increment As Integer For i = 1 To satir_sayisi If ws.Cells(i, "F") = "" Then If bos_hucre Is Nothing Then Set bos_hucre = ws.Cells(i, "F") increment = 1 Else ws.Cells(i, "F").Value = bos_hucre.Offset(-1, 0).Value + increment increment = increment + 1 End If Else Set bos_hucre = Nothing increment = 0 End If Next i End SubBu kod, dolu hücrelerin son artıştan başlayarak değil, her seferinde +1 artarak işlemesi gerektiğini dikkate alarak yazıldı. Bu sayede dolu hücrelerin ardından boş hücreler +1 artacak şekilde değer alacaktır.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.